How do I implement a login screen using the Twitter Kit for Android?
I'm trying to implement a Twitter feature in my custom Android app using their new Twitter Kit 3 API, but I can't seem to get it to work.
I just followed this example: https://dev.twitter.com/twitterkit/android/log-in-with-twitter
My MainActivity class is as follows:
package gib.twitterpruebacliente;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.Twitter;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterLoginButton;
public class MainActivity extends AppCompatActivity {
private TwitterLoginButton loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
Twitter.initialize(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginButton = (TwitterLoginButton) findViewById(R.id.login_button);
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
// Do something with result, which provides a TwitterSession for making API calls
Toast.makeText(MainActivity.this, "IT WORKS!", Toast.LENGTH_SHORT).show();
}
@Override
public void failure(TwitterException exception) {
// Do something on failure
Toast.makeText(MainActivity.this, "FAIL!", Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result to the login button.
loginButton.onActivityResult(requestCode, resultCode, data);
}
}
My main XML activity only has tweeterLogLogButton:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="gib.twitterpruebacliente.MainActivity">
<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
I have already added permission to the manifest:
<uses-permission android:name="android.permission.INTERNET" />
And I added KEYS for my application to the strings folder:
<string name="com.twitter.sdk.android.CONSUMER_KEY">suP4TlwNI7xpwh3sK4x5fKldL</string>
<string name="com.twitter.sdk.android.CONSUMER_SECRET">oaAzoqtMlu5ZtY40LPdzfZTk5wNSTg6Lrnkgx5MPc4TTLbfzMZ</string>
But the app always goes into the crash section. How can I do?
source to share
Well I found a solution. The problem was that I hadn't provided the callback url. To do this, you need to start a Firebase project.
This is achieved by clicking Tools / Firebase / Authentication and following the instructions. The Firebase console then provides a callback url to be specified in Twitter API apps.
source to share