【问题标题】:Google plus intergration using access token使用访问令牌的 Google plus 集成
【发布时间】:2015-07-23 09:43:10
【问题描述】:

在我的android应用程序中,我需要集成Google+登录。我已经尝试过这种方法,我可以成功获取访问令牌。但是在传递到我的网络服务时,我无法在我的访问令牌中获取电子邮件地址。

过去三天我都在谷歌上搜索过,我尝试了很多方法,但都没有找到解决问题的方法。我已经尝试了所有没有在我的访问令牌中获得电子邮件地址的范围。

String mScope = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME;
String exchangeCode = GoogleAuthUtil.getToken(getApplicationContext(), Plus.AccountApi.getAccountName(mGoogleApiClient), mScope);

我在这里获取访问令牌,而不是我的令牌中的电子邮件地址

String exchangeCode = GoogleAuthUtil.getToken(
                         SigninScreen.this,
                         Plus.AccountApi.getAccountName(mGoogleApiClient) + "",
                         "oauth2:"
                           + Scopes.PLUS_ME + " "
                           + "https://www.googleapis.com/auth/plus.login" + " "
                           + "https://www.googleapis.com/auth/plus.me" + " "
                           + "https://www.googleapis.com//auth/plus.profile.emails.read" + " "
                           + "https://www.googleapis.com/auth/userinfo.profile");

我遇到这样的错误

 com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission

这种方式我也试过了

accessToken = GoogleAuthUtil.getToken(
                                   MainActivity.this,
                                   Plus.AccountApi.getAccountName(mGoogleApiClient) + "",
                                   "oauth2:"
                                     + Scopes.PROFILE + " "
                                     + "https://www.googleapis.com/auth/plus.login" + " "
                                     + "https://www.googleapis.com/auth/plus.profile.emails.read");

我用这个方法出错了

Client error response [url] https://www.googleapis.com/plus/v1/people/me?prettyPrint=false [status code] 403 [reason phrase] Forbidden

谁能告诉我我做错了什么?我需要从 Google Plus 获取访问令牌并将令牌传递给我的网络服务并获取详细信息。

非常感谢。

【问题讨论】:

    标签: android google-api google-plus


    【解决方案1】:

    试试这个...

    在 OnCreate..

    mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).addApi(Plus.API)
                    .addScope(Plus.SCOPE_PLUS_LOGIN).build();
    

    在Activity中添加这三个方法...调用登录和需要时退出

     private void signInWithGplus() {
                Log.i("call", "signinwithgoogle");
                mGoogleApiClient.connect();
                if (!mGoogleApiClient.isConnecting()) {
                    mSignInClicked = true;
                    resolveSignInError();
                }
            }
    
            private void resolveSignInError() {
                Log.i("call", "resolvesigninerror");
                if (mConnectionResult.hasResolution()) {
                    try {
                        mIntentInProgress = true;
                        mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
    
                    } catch (SendIntentException e) {
                        mIntentInProgress = false;
                        mGoogleApiClient.connect();
                    }
                }
            }
    
            static void signOutFromGplus() {
                Log.i("call", "signoutfromgoogle");
                if (mGoogleApiClient.isConnected()) {
                    Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                    mGoogleApiClient.disconnect();
    
                    // Toast.makeText(getApplicationContext(), "sign out from google",
                    // Toast.LENGTH_SHORT).show();
                    storeUserData.setBoolean(AppConstants.KEY_IS_LOGIN, false);
                    updateUI(false);
                }
            }
    

    还添加以进行身份​​验证。通过这种方法,您将获得完整的个人资料信息:

     private void getProfileInformation() {
                try {
    
                    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                        Person currentPerson = Plus.PeopleApi
                                .getCurrentPerson(mGoogleApiClient);
    
                        googleFirstName = currentPerson.getDisplayName();
                        googleImage = currentPerson.getImage().getUrl();
                        // String personGooglePlusProfile = currentPerson.getUrl();
                        googleEmailId = Plus.AccountApi
                                .getAccountName(mGoogleApiClient);
                        googleId = currentPerson.getId();
                        Log.i("googleId", googleId);
                        JSONObject fullname = new JSONObject(currentPerson.getName()
                                + "");
                        googleFirstName = fullname.getString("familyName");
                        googleLastName = fullname.getString("givenName");
    
                        if (currentPerson.getGender() == 0)
                            googleGender = "female";
                        else if (currentPerson.getGender() == 1)
                            googleGender = "male";
                        else
                            googleGender = "other";
                        // by default the profile url gives 50x50 px image only
                        // we can replace the value with whatever dimension we want by
                        // replacing sz=X
                        googleImage = googleImage
                                .substring(0, googleImage.length() - 2)
                                + PROFILE_PIC_SIZE;
    
                        new GetGoogleAuthTask().execute();
                        // new LoadProfileImg(null).execute(personPhotoUrl);
    
                    } else {
                        // Toast.makeText(getApplicationContext(),
                        // "Person information is null", Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
    
                }
            }
    
            SharedPreferences SharedPreference;
            Editor editor;
    
            private class GetGoogleAuthTask extends AsyncTask<Void, Void, String> {
                @Override
                protected String doInBackground(Void... params) {
                    String token = null;
    
                    try {
                        token = GoogleAuthUtil.getToken(RegisterActivity.this,
                                Plus.AccountApi.getAccountName(mGoogleApiClient),
                                "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME);
                                // Change the permissions as per your need.
                    } 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) {
    
                    if (token != null) {
                        googleToken = token;
                        // Log.i(TAG, "Access token retrieved:" + token);
                        SharedPreference = getApplicationContext()
                                .getSharedPreferences("TokenPreference", 0);
                        editor = SharedPreference.edit();
                        editor.putString("access_token", token);
                        editor.commit();
                    }
                    Log.i("GooGle", "called");
                    loginWithGoogleData();
                    storeUserData.setBoolean(AppConstants.KEY_IS_LOGIN, true);
                }
    
            }
    

    最后是onactivity结果

     @Override
            public void onActivityResult(int requestCode, int responseCode, Intent data) {
                super.onActivityResult(requestCode, responseCode, data);
                uiHelper.onActivityResult(requestCode, responseCode, data);
    
                /******** GOOGLE CODE START **************/
                if (requestCode == RC_SIGN_IN) {
                    if (responseCode != RESULT_OK) {
                        mSignInClicked = false;
                    }
    
                    mIntentInProgress = false;
    
                    if (!mGoogleApiClient.isConnecting()) {
                        mGoogleApiClient.connect();
                    }
                }
                /******** GOOGLE CODE END **************/
            }
    

    不要忘记在清单中添加权限...

    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.USE_CREDENTIALS"/>
    

    【讨论】:

    • 我也尝试过这个范围,但它没有在访问令牌中返回电子邮件 ID
    • googleEmailId 变量在 getProfileInfo 方法中返回 emailId。 :)
    【解决方案2】:

    我终于在 3 天后得到了解决方案。

    我已经改变了范围。现在它工作正常。我可以从访问令牌中获取所有详细信息,甚至是电子邮件 ID。

    protected String doInBackground(String... args) {
    
    
                String token = null;
                String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + "https://www.googleapis.com/auth/userinfo.email" + " https://www.googleapis.com/auth/plus.profile.agerange.read";
                try {
                    token = GoogleAuthUtil.getToken(
                            SigninScreen.this,
                            Plus.AccountApi.getAccountName(mGoogleApiClient),
                            scope);
                    System.out.println("OKAY!"+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, 125);
                } 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;
    

    【讨论】:

      猜你喜欢
      • 2015-02-22
      • 2014-05-28
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 2014-07-23
      • 2013-11-02
      相关资源
      最近更新 更多