skip to Main Content

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?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. // token from Apple with authResult.getCredential().
    OAuthCredential authCredential= (OAuthCredential) authResult.getCredential();
    

    Call below method to get the access token

    String accessToken=authCredential.getAccessToken()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search