How to get refreshToken when using GoogleAuthUtil
Asked Answered
C

3

8

I'm using GoogleAuthUtil in Google Play Services on Android. After calling GoogleAuthUtil.getToken(context, userName, scope), I got a token like this:

ya29.wQBWztab5kcgMLcMbAI0LwFzHC_DPrxauSWbX4P6KOnBEOgjcm9V7OI9AFr6JGxDY54gP00RemzzgML56_gWRHn8Q5jK16BLY-0y83Gc5vfe3xN-QpyM4d7z

This is an access_token, which can be used in calling Google Apis. Then, how can I get a refresh token to refresh this access_token, because I also use Google oauth java library and YouTube Java Library in my Android project, I want to use these two libraries to maintain/manage the access_token, refresh token and expires_in values. (When using Google oauth java library, the TokenResponse it returned contains access_token, refresh token and expires_in)

Thanks in advance.

Corvette answered 17/11, 2014 at 9:39 Comment(0)
M
3

You cannot directly get a refreshToken using GoogleAuthUtil.getToken() but if you call getToken() each time you get a 401 error, GoogleAuthUtil will return you a new valid token if needed.

Mica answered 22/11, 2014 at 14:25 Comment(2)
Could you elaborate on it? Maybe a useful link on how to handle this?Fidler
Sorry can't remember the details about this anymore :-/. I remember looking for a way to get a refresh_token but did not find one so I just called getToken as described there: developers.google.com/android/guides/http-authMica
E
3

In order to get a refresh token, make sure that your scope is in the following format:

Account account = new Account(mEmail, GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
mScope="oauth2:server:client_id:"+ OAUTH_WEBCOMPONENT_ID+":api_scope:"+"https://www.googleapis.com/auth/userinfo.email";
return GoogleAuthUtil.getToken(mActivity, account, mScope);

This will give you an authorization code, which can be sent to your web component.

Your webcomponent than can use this authorization code only once to get an access token and refresh token with this code. You have to save the refresh token in your database, so that when the access code is no longer valid you can get a new access token when needed.

POST /oauth2/v3/token HTTP/1.1
Host: www.googleapis.com
Content-length: 233
content-type: application/x-www-form-urlencoded
user-agent: google-oauth-playground

code=4%2FVL2YMuPMheOP2-0vyKBSfGd-4er5GsMY17Ecp8ITK4U&redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&client_id=407408718192.apps.googleusercontent.com&client_secret=************&scope=&grant_type=authorization_code

You can simulate how this works here:

https://developers.google.com/oauthplayground/

Eyeopening answered 14/3, 2016 at 4:58 Comment(0)
E
1

Call requestServerAuthCode(String, true) instead requestServerAuthCode(String) which forces the request to include a refresh_token when it succeeds.

https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInOptions.Builder.html#requestServerAuthCode(java.lang.String, boolean)

val task = GoogleSignIn.getSignedInAccountFromIntent(data);
task.addOnSuccessListener {

val account = task.getResult(ApiException::class.java)
val authCode = account!!.serverAuthCode

// Send authcode to server to exchange access and refresh tokens.
exchangeAuthCodeForAccessToken(authCode)

}
Eurhythmic answered 3/9, 2019 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.