79229640

Date: 2024-11-27 09:33:27
Score: 1
Natty:
Report link

thank you for your response. In the meanwhile I could solve my problems and you are right, the time in my emulator was not set correctly. But this was not the main reason for the refresh issue.

  1. I addded "requestOfflineAccess" to the AuthorizationRequest to get a "serverAuthCode"
val authorizationRequest = AuthorizationRequest.Builder()
    .requestOfflineAccess(getString(R.string.default_web_client_id), true)
    .setRequestedScopes(requestedScopes)
    .build()
  1. With the "serverAuthCode" I requested an access/refresh Token. Here I needed to setup a "clientSecret" in the Google Cloud console to get it working.
val tokenResponse = GoogleAuthorizationCodeTokenRequest(
    GoogleNetHttpTransport.newTrustedTransport(),
    GsonFactory.getDefaultInstance(),
    "https://oauth2.googleapis.com/token",
    getString(R.string.default_web_client_id),
    getString(R.string.clientSecret),
    authorizationResult.serverAuthCode,
    getString(R.string.redirectUri)
).execute()
tokenResponse?.let {
    val accessToken = AccessToken(it.accessToken, Date(System.currentTimeMillis() + it.expiresInSeconds * 1000))
    val refreshToken = it.refreshToken

}
  1. Now I was able the use the "UserCredential" Class that supports token refresh different to "OAuth2Credentials", that does not support refreshing.
var userCredentials = UserCredentials.newBuilder()
    .setClientId(getString(R.string.default_web_client_id))
    .setClientSecret(getString(R.string.clientSecret))
    .setAccessToken(accessToken)
    .setRefreshToken(refreshToken)
    .build()
userCredentials?.let {
    driveService = Drive.Builder(
        GoogleNetHttpTransport.newTrustedTransport(),
        GsonFactory.getDefaultInstance(),
        HttpCredentialsAdapter(it)
    )
        .setApplicationName(getString(R.string.applicationName))
        .build()
}
  1. before usind the driveService, I always call:
userCredentials?.refreshIfExpired()

The documentation from Google is very poor. Especially I am still not 100% sure, if the driveService object takes into account the token changes of the userCrediatials object after its creation and if the object needs be rebuilt or not. More precise, I wonder if the HttpCredentialsAdapter(userCredentials) is called by reference or by value.

Reasons:
  • Blacklisted phrase (0.5): thank you
  • Blacklisted phrase (0.5): I need
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: spiderbaby