Some of the users of my Android app are experiencing an error like this after 1 hour of usage:
Error loading file preview: com.google.api.gax.rpc.UnauthenticatedException: io.grpc.StatusRuntimeException: UNAUTHENTICATED: Request had invalid authentication credentials. Expected OAuth 2 access token login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
I've determined that I need a refresh token, but it looks like there's no way to get one on Android. I've found one relevant post here, but the solution doesn't work for me. Even with the proposed solution, the UserCredentials returned by my getUserCredentials() method contains a null refreshToken.
Any help would be appreciated.
fun getGoogleSignInClient(activity: Activity, needsPhotosScope: Boolean, needsDriveScope: Boolean): GoogleSignInClient {
val signInOptions = GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
val scopes = getRequiredGoogleServiceScopes(needsPhotosScope, needsDriveScope)
when (scopes.size) {
0 -> throw RuntimeException("Attempting to use Google Sign-In without requesting any Scopes.")
1 -> signInOptions.requestScopes(scopes[0])
2 -> signInOptions.requestScopes(scopes[0], scopes[1])
}
return GoogleSignIn.getClient(activity, signInOptions.build())
}
fun getGoogleAccountCredential(googleSignInAccount: GoogleSignInAccount, needsPhotosScope: Boolean,
needsDriveScope: Boolean): GoogleAccountCredential {
// Create list of Scopes in String form.
val scopeStringList = getRequiredGoogleServiceScopes(needsPhotosScope, needsDriveScope).map { it.scopeUri }
val credential = GoogleAccountCredential.usingOAuth2(App.appContext, scopeStringList)
credential.selectedAccount = googleSignInAccount.account
return credential
}
// Fetch a List of Scopes that match the requirements based on the user's current search criteria.
fun getRequiredGoogleServiceScopes(needsPhotosScope: Boolean, needsDriveScope: Boolean): List<Scope> {
val scopes = mutableListOf<Scope>()
if (needsPhotosScope) scopes.add(Scope(googlePhotosScope))
if (needsDriveScope) scopes.add(Scope(DriveScopes.DRIVE))
return scopes
}
Some of the users of my Android app are experiencing an error like this after 1 hour of usage:
I've determined that I need a refresh token, but it looks like there's no way to get one on Android. I've found one relevant post here, but the solution doesn't work for me. Even with the proposed solution, the
UserCredentials
returned by mygetUserCredentials()
method contains a nullrefreshToken
.Any help would be appreciated.