Implementing Google Sign-In with CredentialManager and Google Drive API authorization in Android: What am I missing?

I’m stuck trying to set up Google Sign-In using the new CredentialManager and then authorize access to the Google Drive API in my Android app. Here’s what I’ve done so far:

  1. Created a method to handle sign-in with CredentialManager
  2. Set up a callback to process the sign-in result
  3. Attempted to authorize Google Drive API access

But I’m running into issues:

  • Not sure how to use the googleIdTokenCredential for server validation
  • Getting a null pointer exception when trying to authorize Google Drive API
  • Confused about how to properly link the sign-in process with the Drive API authorization

Here’s a simplified version of my code:

fun signIn() {
  val credentialManager = CredentialManager.create(context)
  val request = GetCredentialRequest.Builder()
    .addCredentialOption(GetGoogleIdOption.Builder().build())
    .build()
  
  credentialManager.getCredentialAsync(
    request,
    object : CredentialManagerCallback<GetCredentialResponse, GetCredentialException> {
      override fun onResult(result: GetCredentialResponse) {
        handleSignInResult(result)
      }
    }
  )
}

fun handleSignInResult(result: GetCredentialResponse) {
  val credential = result.credential as? CustomCredential
  if (credential?.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
    val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credential.data)
    // What to do next?
    authorizeDriveApi()
  }
}

fun authorizeDriveApi() {
  Identity.getAuthorizationClient(context)
    .authorize(AuthorizationRequest.Builder().build())
    .addOnSuccessListener { result ->
      // Getting null account here
      val account = result.toGoogleSignInAccount()
      // How to properly set up Drive API client?
    }
}

Can someone help me understand what I’m doing wrong or missing in this process? How do I correctly link the sign-in with the Drive API authorization?

hey mate, i had similar issues. make sure u add the right scopes in ur google cloud console. for the drive api, use DriveScopes.DRIVE_FILE in ur AuthorizationRequest.

also, dont forget to handle errors properly. google’s docs can be confusing af sometimes, but keep at it!

I’ve encountered similar challenges with Google Sign-In and Drive API integration. Here’s what you might be missing:

For server validation, send the ID token (googleIdTokenCredential.idToken) to your backend. Your server should verify this using Google’s tokeninfo endpoint.

Your Drive API authorization is failing because you’re mixing old and new APIs. Try this approach instead:

After successful sign-in, use the Identity API to get an access token for Drive:

Identity.getAuthorizationClient(context).authorize(
AuthorizationRequest.Builder()
.setRequestedScopes(listOf(DriveScopes.DRIVE_FILE))
.build()
)
.addOnSuccessListener { result →
val accessToken = result.accessToken
// Initialize Drive API client with this token
}

This should resolve your null pointer exception and properly link sign-in with Drive API authorization. Don’t forget to add necessary scopes in Google Cloud Console and handle potential errors in your implementation.

I’ve recently gone through a similar implementation, and I can share some insights that might help you out.

For server validation of the googleIdTokenCredential, you’ll want to send the ID token to your backend. You can get it with googleIdTokenCredential.idToken. Your server can then verify this token using Google’s tokeninfo endpoint or a Google auth library.

Regarding the null pointer exception in Drive API authorization, it looks like you’re mixing up the old GoogleSignInAccount with the new Identity API. Instead, try using the AccessToken directly:

fun authorizeDriveApi(googleIdTokenCredential: GoogleIdTokenCredential) {
    val request = AuthorizationRequest.Builder()
        .setRequestedScopes(listOf(DriveScopes.DRIVE_FILE))
        .build()
    
    Identity.getAuthorizationClient(context).authorize(request)
        .addOnSuccessListener { result ->
            val accessToken = result.accessToken
            // Use this accessToken to initialize your Drive API client
        }
}

Call this function from your handleSignInResult, passing the googleIdTokenCredential. This should link your sign-in process with Drive API authorization.

Remember to add the necessary scopes in your Google Cloud Console and handle error cases in your code. Hope this helps!