【问题标题】:Getting user credentials using Google+ API使用 Google+ API 获取用户凭据
【发布时间】:2015-08-30 20:32:12
【问题描述】:

我正在尝试使用 Google+ Api 在我的 Android 应用程序中包含 Google 登录我能够从用户那里获取帐户详细信息,但一旦登录我就会得到null请求用户名时使用调用:

Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getDisplayName()

而Logcat显示:

BasicNetwork.performRequest:意外响应代码 403 https://www.googleapis.com/plus/v1/people/me

虽然我可以使用以下方式获取用户的电子邮件:

Plus.AccountApi.getAccountName(GoogleClient.mGoogleApiClient)

请帮助我发现我的错误

【问题讨论】:

    标签: android google-app-engine google-api google-api-java-client


    【解决方案1】:

    我已经尝试了相同的代码并且它工作正常! 因此,只需确保两件事:

    1) 在 Google API 控制台中注册您的数字签名 .apk 文件的公共证书。链接如下:

    https://developers.google.com/+/mobile/android/getting-started#step_1_enable_the_google_api

    2)确保您已添加 google+ api 访问权限并已使用 SHA1 创建客户端密钥。
    休息就好了。

    【讨论】:

    • 我想知道我可以跳过添加配置文件的部分,而是可以在 Android 清单中提及 API 密钥吗?
    • @sv_jan5 哪个配置文件?只需按照文档步骤 - 在 google 控制台上添加项目,启用 g+ api,使用 SHA 生成密钥,将 gms 库添加到您的 android 项目并使用示例代码配置 G+。就是这样
    • acutally 我正在使用 android studio 来构建我的应用程序,所以我遵循本指南 developers.google.com/identity/sign-in/android/… 来集成 google 登录。这里提到了包含一个配置文件
    • @sv_jan5 我也只使用 Android Studio,我已经使用上面的链接实现了。它工作完美且易于遵循步骤。按照上面的链接,无论您遇到什么困难,我都可以为您提供帮助。
    • 谢谢!我只想知道您提到的链接,我们是否以 API 密钥等形式向应用程序提供一些身份验证信息?
    【解决方案2】:

    我认为应用程序中 google+ 登录集成的基本步骤您都知道。以下是其余步骤

    第一步—— 在 oncreate 中

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

    第 2 步—— 在登录按钮点击监听器调用这个----

    private void LoginGoogle(){
            int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
            if (errorCode != ConnectionResult.SUCCESS) {
              GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
            }
            else{
                //perform login
                if (!mGoogleApiClient.isConnecting()) {
                    mSignInClicked = true;
                    mGoogleApiClient.connect();
    
                }
            }
        }
    

    第 3 步---- 在 onActivityresult 中

    if (requestCode == RC_SIGN_IN) { 
                    if (resultCode != RESULT_OK) {
                      mSignInClicked = false;
                }
    
                mIntentInProgress = false;
    
                if (!mGoogleApiClient.isConnected()) {
                  mGoogleApiClient.reconnect();
                }
              }
    
    step 4-----
    
        @Override
        public void onConnectionFailed(ConnectionResult result) {
            // TODO Auto-generated method stub
            if(!mIntentInProgress){
                if ( mSignInClicked && result.hasResolution()) {
                     // The user has already clicked 'sign-in' so we attempt to resolve all
                      // errors until the user is signed in, or they cancel.
                      try {
                        result.startResolutionForResult(this, RC_SIGN_IN);
                        mIntentInProgress = true;
                      } catch (SendIntentException e) {
                        // The intent was canceled before it was sent.  Return to the default
                        // state and attempt to connect to get an updated ConnectionResult.
                        mIntentInProgress = false;
                        mGoogleApiClient.connect();
                      }
    
                  }
            }
        }
        @Override
        public void onConnected(Bundle arg0) {
            // TODO Auto-generated method stub
            mSignInClicked = false;
            getProfileInformation();
        }
        @Override
        public void onConnectionSuspended(int arg0) {
            // TODO Auto-generated method stub
            mGoogleApiClient.connect();
        }
    
    
        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
            if (mGoogleApiClient.isConnected()) {
                  mGoogleApiClient.disconnect();
                }
    
        }
    

    第五步-----

    private void getProfileInformation(){
            try {
                if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                    Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
                    String id=currentPerson.getId();
                    String personName = currentPerson.getDisplayName();
                    String personPhoto = currentPerson.getImage().getUrl();
                    String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
    
                    String profilePic=personPhoto.substring(0,
                            personPhoto.length() - 2)
                            + ProfilePicSize;
    
                    Log.e("GOOGLE", id);
                    Log.e("GOOGLE", personName);
                    Log.e("GOOGLE", profilePic);
                    Log.e("GOOGLE",email);
    
    
                  }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2019-02-11
      • 2019-01-13
      • 1970-01-01
      • 2015-03-02
      • 2014-12-18
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多