2

I'm updating my app to use the new Credential Manager API for authenticating users via Firebase with Google Sign-In, since the previous approach using GoogleSignIn.getSignedInAccountFromIntent is now deprecated in Jetpack Compose.

Previously, we used:

GoogleSignIn.getSignedInAccountFromIntent(intent)

Now I'm using the updated recommended approach:

val googleIdOption = GetGoogleIdOption.Builder()
    .setFilterByAuthorizedAccounts(false)
    .setAutoSelectEnabled(false)
    .setServerClientId(BuildConfig.GOOGLE_SIGN_IN_SERVER_CLIENT_ID)
    .build()

val request = GetCredentialRequest.Builder()
    .addCredentialOption(googleIdOption)
    .build()

val credentialManager = CredentialManager.create(context)
val operation: Either<Throwable, GetCredentialResponse> = catch {
    credentialManager.getCredential(context, request)
}

This correctly launches the sign-in flow and returns a GetCredentialResponse. However, I can't figure out how to obtain a valid GoogleSignInAccount (or similar session object) from this response to use with the Google Calendar API or other authenticated Google services.

My use case: I need the signed-in Google account to access Google Calendar API using the following setup:

val mCredential = GoogleAccountCredential.usingOAuth2(
    context,
    arrayListOf(CalendarScopes.CALENDAR),
).setBackOff(ExponentialBackOff())

mCredential.selectedAccount = GoogleSignIn.getLastSignedInAccount(context)?.account

val transport = NetHttpTransport()
val jsonFactory = JacksonFactory.getDefaultInstance()

val calendarService = Calendar.Builder(transport, jsonFactory, mCredential)
    .setApplicationName(context.getString(R.string.app_name))
    .build()

But GoogleSignIn.getLastSignedInAccount(context) is deprecated, and I can't find an equivalent from the Credential Manager API. So I’m stuck between:

  • The modern sign-in flow (Credential Manager)
  • And the older account retrieval method (GoogleSignIn.getLastSignedInAccount) that’s deprecated

What I need help with:

  • How can I retrieve a valid Account or GoogleSignInAccount from the new Credential Manager API response?

  • Is there an alternative way to use Google Calendar API that works with Credential Manager?

  • Do I need to manually exchange an ID token for an access token? If yes, how?

Any help or official pattern for migrating Calendar API usage would be appreciated!

2
  • Have you tried asking for support from Google Play? Commented Jun 24 at 8:11
  • @PatrickdC Yes! Commented Jun 24 at 9:10

1 Answer 1

2

When you use CredentialManager to authenticate a user, you get an ID Token, inside which you can find the user's email address. In most cases, you can also use getId() to also get the email directly from the response. However, an ID Token is not enough to get access to any of the Google APIs, you need an access token, which means you need to use the Authorization APIs. That gives you an Access Token that you can then use for accessing Calendar APIs, assuming that you request the appropriate scopes in your Authorization request.

Additional info: In order to access most Google APIs, for example Calendar APIs, you can use the following pattern, once you have an access token (hence no reliance on the deprecated APIs):

import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
...

GoogleCredentials credentials =
        GoogleCredentials.create(new AccessToken(accessToken, null));
Calendar calendarService = new Calendar.Builder(
            new NetHttpTransport(),
            new GsonFactory(), 
            new HttpCredentialsAdapter(credentials))
        .setApplicationName("<Your App Name")
        .build();
Sign up to request clarification or add additional context in comments.

4 Comments

But I want to use it inside below Calendar API in Jetpack directly
I am not sure what you mean here. Earlier, I mentioned how you can obtain an access token, if you want to access the Calendar APIs. If you have specific questions, please elaborate.
Please refer to my question again, I asked about how to access calendar service object which requires Google Account object to work, and I'm accessing it even when app is in background, so currently I've given account object using GoogleSignIn.getLastSignedInAccount which is now deprecated
I added more info to the original answer so you can see you do not need to use any of the deprecated APIs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question