我有一个通过请求一次性授权码来访问谷歌服务的安卓应用程序。每次用户登录时,都会从 Google 检索授权码。它工作了一段时间,但停止为特定用户工作。该用户不断收到UserRecoverableAuthException
因此 Google 的权限请求屏幕不断显示。当我尝试获取该用户的正常访问令牌时,没有问题。
我使用的代码:
protected Void doInBackground(Void... params)
{
try
{
// Obtaining normal access token for testing propose.
String tempToken = GoogleAuthUtil.getToken(getApplicationContext(), mAccountName, "oauth2:https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/calendar");
NLog.i(TAG, "Got token2: "+tempToken);
// Obtaining One time auth code
String scope = String.format("oauth2:server:client_id:%s:api_scope:%s",CLIENT_ID, TextUtils.join(" ", SCOPES));
mServerToken = GoogleAuthUtil.getToken(getApplicationContext(), mAccountName, scope);
NLog.i(TAG, "Got code: "+mServerToken);
sendGoogleLoginResult(RESULT_OK, null, mServerToken);
finish();
}
catch (UserRecoverableAuthException e)
{
if (e !=null && e.getIntent() != null && authExceptionCount <3)
{
authExceptionCount++;
Intent recoveryIntent = e.getIntent();
startActivityForResult(recoveryIntent, GOOGLE_RECOVERY_ACTIVITY_REQUEST);
}
else
{
Log.i(TAG, "UserRecoverableAuthException but there was no intent: ", e);
sendGoogleLoginResult(RESULT_CANCELED, null, mServerToken);
finish();
}
}
catch (IOException e) {
Log.i(TAG, "transient error encountered: ", e);
sendGoogleLoginResult(RESULT_CANCELED, null, mServerToken);
finish();
} catch (GoogleAuthException e) {
// This is likely unrecoverable.
Log.e(TAG, "Unrecoverable authentication exception: " + e.getMessage(), e);
sendGoogleLoginResult(RESULT_CANCELED, null, mServerToken);
finish();
}
return null;
}