如果使用ID令牌到期时间来确定会话生存期,则应在每次API调用应用程序服务器之前调用silentSignIn来检索刷新的ID令牌.
我试图通过调用silentSignIn来获取新的令牌.但是,我总是得到相同的过期ID令牌.有人可以帮我指出正确的文档,说明如何强制刷新以获取新令牌.我的代码没什么特别的.它几乎与谷歌样本中的相同.
private void buildGoogleApiClient()
{
// Configure sign-in to request the user's ID, email address, and basic profile. ID and
// basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
//.requestServerAuthCode(serverClientId)
.requestIdToken(SERVER_ID)
.requestScopes(new Scope(Scopes.PLUS_LOGIN))
.requestEmail()
.build();
// Build a GoogleApiClient with access to GoogleSignIn.API and the options above.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
Run Code Online (Sandbox Code Playgroud)
我按上述方法构建了API,当用户点击使用google按钮登录时,我执行以下操作
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone())
{
// If the user's cached credentials are valid, the …Run Code Online (Sandbox Code Playgroud) 我从通过 google 登录 android 收到的令牌中收到错误消息:
{“错误”:“invalid_token”,“error_description”:“无效值”}
我还注意到,与我在 iOS 中获得的令牌相比,我的令牌看起来有点短:
ya29.4AFYx2XNZ1sdfdzhWo-of-fSpsDPjgmATx-J82mCjqZJXglwj8VOvedpY_YXJgEVIWe
Run Code Online (Sandbox Code Playgroud)
我得到这样的令牌:
private class RetrieveTokenTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String accountName = params[0];
String scopes = "oauth2:profile email";
String token = null;
try {
token = GoogleAuthUtil.getToken(getActivity().getApplicationContext(), accountName, scopes);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} catch (UserRecoverableAuthException e) {
//startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED);
} catch (GoogleAuthException e) {
Log.e(TAG, e.getMessage());
}
return token;
}
@Override
protected void onPostExecute(String token) {
super.onPostExecute(token);
Log.i("Token Value: ", …Run Code Online (Sandbox Code Playgroud)