Google Oauth2无法检索一次性授权代码

时间:2013-10-07 13:43:19

标签: android oauth-2.0 google-oauth

我有一个Android应用程序,通过请求一次性授权代码来获取对Google服务的访问权限。每次用户登录时,都会从Google检索身份验证代码。 它运作良好一段时间,但停止为特定用户工作。 此用户不断收到UserRecoverableAuthException所以Google Googles权限请求屏幕会一次又一次地显示。 当我尝试获得该用户的正常访问令牌时,没有问题。

我使用的代码:

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;
        }

0 个答案:

没有答案