我可以将 Android 设备上的一次性使用令牌换成访问令牌 and a 刷新令牌。我正在尝试弄清楚如何使用刷新令牌.
I found this https://developers.google.com/accounts/docs/OAuth2WebServer#refresh它通过 HTTPS 请求工作,但我想知道 Java SDK 中是否有某个地方可以处理刷新?
你不需要。只需在每次 HTTP 会话之前调用 GoogleAuthUtil.getToken(),GoogleAuthUtil 将确保您获得一个有效的会话,并在必要时刷新。
编辑:哦,好的,他正在服务器上执行此操作。下面是一些使用刷新令牌的 java 代码:
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");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)