使用Google+ API获取用户凭据

时间:2015-06-15 11:06:08

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

我正在尝试使用 Google+ Api在我的Android应用程序中加入 Google登录我可以从用户那里获取帐户详细信息,但一旦登录我就会收到 null 请求用户名时使用call:

  

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

Logcat显示:

  

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

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

  

Plus.AccountApi.getAccountName(GoogleClient.mGoogleApiClient)

请帮我发现我的错误

2 个答案:

答案 0 :(得分:3)

我尝试过相同的代码并且工作正常! 所以,只需确保两件事:

1)在Google API Console.Link中注册数字签名的.apk文件的公共证书:

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

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

答案 1 :(得分:1)

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

步骤1-- 在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();
            }

    }

第5步-----

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();
        }
    }
相关问题