检索到的访问令牌:null。 com.google.android.gms.auth.GoogleAuthException:未知

时间:2015-01-27 23:57:39

标签: android google-plus google-play-services google-authentication clientid

我的应用允许用户使用Google Plus登录,并获取他们的姓名和电子邮件地址。我试图访问令牌。

访问令牌的代码:

Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();        
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
        AccountManager am = AccountManager.get(this);

        final Account[] accounts = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);

        AsyncTask<Void, Void, String> task2 = new AsyncTask<Void, Void, String>() {
            public static final int REQUEST_CODE_TOKEN_AUTH = 100;

            @Override
            protected String doInBackground(Void... params) {
                String mScope="audience:server:client_id:899555500747-38rpnq51of946grhdvofck7r8u5p09cd.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/plus.login";
// Get the token for the current user
                String token = null;
                try {
                    token = GoogleAuthUtil.getToken(getApplicationContext(), Plus.AccountApi.getAccountName(mGoogleApiClient), mScope);
                    Log.i("G token", token);
                } catch (IOException transientEx) {
                    // Network or server error, try later
                    Log.e(TAG, transientEx.toString());
                } catch (UserRecoverableAuthException e) {
                    // Recover (with e.getIntent())
                    Log.e(TAG, e.toString());
                    Intent recover = e.getIntent();
                    startActivityForResult(recover, REQUEST_CODE_TOKEN_AUTH );
                } catch (GoogleAuthException authEx) {
                    // The call is not ever expected to succeed
                    // assuming you have already verified that
                    // Google Play services is installed.
                    Log.e(TAG, authEx.toString());
                }
                return token;
            }
            @Override
            protected void onPostExecute(String token) {
                Log.i(TAG, "Access token retrieved:" + token);
            }

        };
        task2.execute();

错误:

01-27 23:42:14.877  30994-31262/com.unicloud.mittal E/loginWithGooglePlus﹕ com.google.android.gms.auth.GoogleAuthException: Unknown
01-27 23:42:14.877  30994-30994/com.unicloud.mittal I/loginWithGooglePlus﹕ Access token retrieved:null

我尝试过各种解决方案,我可以在stackoverflow上找到它。目前,我正在使用dev console中“服务帐户”中的客户端ID,我也尝试将其用于“Android应用程序的客户端ID”,但仍显示相同的错误。

请让我知道我做错了什么?感谢。

1 个答案:

答案 0 :(得分:6)

我通过替换此行解决了这个问题

String mScope="audience:server:client_id:899555500747-38rpnq51of946grhdvofck7r8u5p09cd.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/plus.login";

用这个

String mScope = "oauth2:https://www.googleapis.com/auth/plus.login";

我获得了访问令牌,我想知道我是否做得对。所以我验证了令牌是否正确。 我试图去这个地址,我用我检索的令牌替换了accessToken

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=accessToken

它向我展示了这种输出。

{
  "issued_to": "xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
     "audience": "xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
     "user_id": "xxxxxxxxxxxxxxxxxxxxxxx",
     "scope": "https://www.googleapis.com/auth/userinfo.profile https://gdata.youtube.com",
     "expires_in": 3019,
     "access_type": "online"
    }

issued_to中,它显示了我的Android应用程序的客户端ID,这意味着此令牌会发送到我的客户端ID。所以我认为我走在正确的轨道上。