Google oauth how to use update token

I can exchange my usage time token from my Android device for accessToken and refreshToken. I am trying to figure out how to use refreshToken. I found this https://developers.google.com/accounts/docs/OAuth2WebServer#refresh that works on an https request, but I was wondering if there is somewhere in java sdk to handle the update. I looked, but could not find it.

+4
source share
2 answers

You do not need. Just call GoogleAuthUtil.getToken () before each HTTP session, and GoogleAuthUtil will make sure you get one that works by updating if necessary.

EDITED: Oh well, he does it on the server. Here is some Java code that uses the update token:

String data = "refresh_token=" + mRefreshToken + "&client_id=" + Constants.WEB_CLIENT_ID + "&client_secret=" + Constants.WEB_CLIENT_SECRET + "&grant_type=refresh_token"; byte[] body = data.getBytes(); URL url = new URL(Constants.GOOGLE_TOKEN_ENDPOINT); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setFixedLengthStreamingMode(body.length); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.getOutputStream().write(body); body = XAuth.readStream(conn.getInputStream()); JSONObject json = new JSONObject(new String(body)); String accessToken = json.optString("access_token"); if (accessToken == null) { throw new Exception("Refresh token yielded no access token"); } 
+3
source

Why not use the Google API for this.

 GoogleCredential refreshTokenCredential = new GoogleCredential.Builder().setJsonFactory(JSON_FACTORY).setTransport(HTTP_TRANSPORT).setClientSecrets(CLIENT_ID, CLIENT_SECRET).build().setRefreshToken(yourOldToken); refreshTokenCredential.refreshToken(); //do not forget to call this method String newAccessToken = refreshTokenCredential.getAccessToken(); 
0
source

Source: https://habr.com/ru/post/1501529/


All Articles