Home > Software design >  Can't access OAuth Access token method while doing Github Login with Firebase
Can't access OAuth Access token method while doing Github Login with Firebase

Time:08-22

According to Firebase documentation to get OAuth access token on successful login, we can get it from the getCredential() method like: authResult.getCredential().getAccessToken()

Code: According to firebase:

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().
      }
    })
.addOnFailureListener(
    new OnFailureListener() {
      @Override
      public void onFailure(@NonNull Exception e) {
        // Handle failure.
      }
    });

But, when I try to access the token like that, it just simply says that the method doesn't exist.

 pendingResultTask
    ?.addOnSuccessListener { authResult ->
        val username = authResult.additionalUserInfo?.username
        val accessToken = authResult.getCredential().getAccessToken()

        Toast.makeText(
            activity,
            "username: $username, accessToken: $accessToken",
            Toast.LENGTH_LONG
        ).show()
        navigateToHomeScreen()
    }
    ?.addOnFailureListener { exception ->
        Toast.makeText(
            activity,
            "Something Went Wrong: ${exception.message}",
            Toast.LENGTH_LONG
        ).show()
    }

here in this code, I can't use getAccessToken() method, error: Unresolved reference

Why I am not able to access the getAccessToken() method?

CodePudding user response:

AuthResult#getCredential() method returns an object of type AuthCredential, and as far as I can see, there is no getAccessToken() method present there.

However, 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 a getAccessToken() method.

  • Related