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.
val authorizationRequest = AuthorizationRequest.Builder()
.requestOfflineAccess(getString(R.string.default_web_client_id), true)
.setRequestedScopes(requestedScopes)
.build()
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
}
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()
}
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.