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!