How can I get the token and secret in Firebase Twitter Login?

Viewed 76

I am developing an android app using Firebase Auth. My app should provide a login feature using Twitter. So I am developing Twitter Login using the Firebase Auth.

I should send the Twitter user's token and the secret to our backend server. But I don't know how to get them.

According to the firebase official documents,

firebaseAuth
    .startActivityForSignInWithProvider(/* activity= */ this, provider.build())
    .addOnSuccessListener(
        new OnSuccessListener<AuthResult>() {
          @Override
          public void onSuccess(AuthResult authResult) {
            // User is signed in.
            // IdP data available in
            // authResult.getAdditionalUserInfo().getProfile().
            // The OAuth access token can also be retrieved:
            // authResult.getCredential().getAccessToken().
            // The OAuth secret can be retrieved by calling:
            // authResult.getCredential().getSecret().
          }
        })
    .addOnFailureListener(
        new OnFailureListener() {
          @Override
          public void onFailure(@NonNull Exception e) {
            // Handle failure.
          }
        });

onSuccess callback returns AuthResult But when I try to use below function:

authResult.getCredential().getAccessToken()
authResult.getCredential().getSecret()

There are no functions! How can I get them???

1 Answers

The AuthResult class contains a method called getCredential(), which returns an object of type AuthCredential. However, this class, does not contain any getAccessToken(), or getSecret() methods. There is also no super class that might hold them.

When you try to implement a sign-in mechanism with a provider, there are two operations that you should do:

  1. Get provider credentials.
  2. Go forward and perform the authentication. If you do it with Firebase, then you should pass them to the FirebaseAuth#signInWithCredential(AuthCredential) method.

If you need to get the values of the token and the secret, you have to get them from inside a TwitterAuthToken object. As you can see, this class exposes two public fields called token and secret.

So once you press a sign-in button and choose an account, you'll get as a result an object of type Result. As you can see, this is a generic class. This means that the data field inside the class is of type TwitterSession, which is a class that extends Session. Now you'll be able to get both objects like this:

String token = session.getAuthToken().token;
String secret = session.getAuthToken().secret;

Now you can go forward and create an AuthCredential object:

AuthCredential credential = TwitterAuthProvider.getCredential(token, secret);

And perform the Firebase authentication with Twitter:

auth.signInWithCredential(credential).addOnCompleteListener(/* ... */);

Edit:

According to your comment:

Twitter stopped supporting the SDK 4 years ago.

You're absolutely right. Getting back to Firebase, please note that there is a commented line there that says:

The OAuth access token can also be retrieved.

So you should take a look into OAuthCredential class, which exposes the getAccessToken() and getSecret() methods.

Related