【问题标题】:Access token retrieved:null. com.google.android.gms.auth.GoogleAuthException: Unknown检索到的访问令牌:null。 com.google.android.gms.auth.GoogleAuthException:未知
【发布时间】:2015-03-26 18:20:26
【问题描述】:

我的应用允许用户使用 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”,它仍然显示相同的错误。

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

【问题讨论】:

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


    【解决方案1】:

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

    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。所以我认为我在正确的轨道上。

    【讨论】:

      猜你喜欢
      • 2014-09-27
      • 2018-03-01
      • 1970-01-01
      • 2021-09-05
      • 2015-09-08
      • 2020-10-12
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多