Twitter Login for Android

In this guide, we will allow people to use Twitter as a login service. This tutorial requires that you configure your parse libs in a different way than that described in the Quickstart. Let’s start first creating a Twitter App.

Step 1 – Create a Twitter app

Go to your Twitter Apps page and create a new App. Make sure you fill all of the following fields, otherwise the API might not work as expected:

twitter_create

After that, your app will be created at Twitter and you’ll see a screen with some tabs. Go to the ‘Keys and Access Tokens’ tab as follows:

twitter_tabs

Then find your Consumer Key and your Consumer Secret, as follows:

twitter_keys

Write your Consumer Key and your Consumer Secret down, as we will use it later. Now, we need to configure a few parameters at Back4App.

Step 2 – Add Twitter keys to Back4App

Go to your Dashboard and find the following option:

twitter_dashboard

Click on it and you’ll see a screen that requires your Consumer Key and your Consumer Secret:

twitter_dashboard_keys

Write each key on its respective field, hit ‘Save’ and you’re set on Back4App! Now, it is necessary to configure your App.

Step 3 – Configure your App

It is necessary to add a few libraries that enable your project to use Twitter with Parse API. First of all, it is necessary to remove the following library:

compile 'com.parse:parse-android:1.+'

You should replace that line with this one:

compile 'com.parse:parsetwitterutils-android:1.10.+'

If you add both to your project, it will launch an Exception on runtime and won’t work. Now, you have to initialize ParseTwitterUtils. Right below your Parse initialization, add:

ParseTwitterUtils.initialize("<Your Consumer Key>", "<Your Consumer Secret>");

And you’re set to use the Twitter API on your App. We’ll show how to login using twitter on your app.

Step 4 – Login with Twitter

After initializing, we can use the following code to login:

ParseTwitterUtils.logIn(view.getContext(), new LogInCallback() {
    @Override
    public void done(ParseUser user, ParseException err) {
        if (user == null) {
            Log.d("MyApp", "Uh oh. The user cancelled the Twitter login.");
        } else if (user.isNew()) {
            Log.d("MyApp", "User signed up and logged in through Twitter!");
        } else {
            Log.d("MyApp", "User logged in through Twitter!");
        }
    }
});

And that’s it for the Twitter Login!