在安卓中无法从Google+获取登录用户信息

4

我正在使用Google+登录我的应用程序。当我点击Google按钮时,会调用OnConnect()方法,但是我从OnConnect()方法中检索有关Google的一些信息,但“Plus.PeopleApi.getCurrentPerson(mGoogleApiClient)”方法返回null值。请帮助我。

// Google API Services CallBack Method
        private boolean mSignInClicked;
        private ConnectionResult mConnectionResult;

        private void loginWithGoogle() {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).addApi(Plus.API, null)
                    .addScope(Plus.SCOPE_PLUS_PROFILE)
                    .addScope(Plus.SCOPE_PLUS_LOGIN).build();

            mGoogleApiClient.connect();
        }

        @Override
        public void onConnected(Bundle arg0) {
            mSignInClicked = false;
            if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                Person currentPerson = Plus.PeopleApi
                        .getCurrentPerson(mGoogleApiClient);

                String personName = currentPerson.getDisplayName();
                Log.i("personName", personName);

                Image personPhoto = currentPerson.getImage();
                String photoUrl = personPhoto.getUrl();
                Log.i("photoUrl", photoUrl);

                String personGooglePlusProfile = currentPerson.getUrl();
                Log.i("personGooglePlusProfile", personGooglePlusProfile);

                String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
                Log.i("email", email);

                boolean hasBirthday = currentPerson.hasBirthday();
                Log.i("hasBirthday", "" + hasBirthday);

                String birthday = currentPerson.getBirthday();
                Log.i("birthday", "" + birthday);

                boolean hasAbout = currentPerson.hasAboutMe();
                Log.i("hasAbout", "" + hasAbout);

                String about = currentPerson.getAboutMe();
                Log.i("about", "" + about);

                String Id = currentPerson.getId();
                Log.i("Id", Id);

                int gender = currentPerson.getGender();
                Log.i("gender", "" + gender);

                boolean hasLocation = currentPerson.hasCurrentLocation();
                Log.i("hasLocation", "" + hasLocation);

                String location = currentPerson.getCurrentLocation();
                Log.i("location", "" + location);

                AgeRange agerange = currentPerson.getAgeRange();
                if (agerange != null)
                    Log.i("agerange", "" + agerange.toString());

                Name name = currentPerson.getName();
                Log.i("getFamilyName", name.getFamilyName());

                btnGoogle = true;

                new ApiCall().execute(url);

                if (mGoogleApiClient.isConnected()) {
                    Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                    mGoogleApiClient.disconnect();
                }
            } else {
                Log.i("personName", "getCurrentPerson(mGoogleApiClient) is null");
            }
        }

        @Override
        public void onConnectionSuspended(int arg0) {
            mGoogleApiClient.connect();
        }

        @Override
        public void onConnectionFailed(ConnectionResult result) {
            if (!mIntentInProgress) {
                mConnectionResult = result;
                if (mSignInClicked) {
                    // The user has already clicked 'sign-in' so we attempt to
                    // resolve
                    // all
                    // errors until the user is signed in, or they cancel.
                    resolveSignInError();
                }
            }
        }

        public void resolveSignInError() {
            if (mConnectionResult.hasResolution()) {
                try {
                    mIntentInProgress = true;
                    mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
                } 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();
                }
            }
        }

你是否已经添加了 Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this); - SweetWisher ツ
在哪里?在 onConnect() 方法中吗? - Umesh Kumar Saraswat
在“OnConnected”中是“是” - SweetWisher ツ
请给我完整的 onConnect() 方法,先行致谢。 - Umesh Kumar Saraswat
你能从Google Plus获取出生日期吗? - Shadow
你好!你使用哪个库来获取“Plus”类? - A. N
3个回答

1
你需要在onConnected中添加这一行:Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this); 像这样:
@Override
public void onConnected(Bundle connectionHint) {

    /* This Line is the key */
    Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);

    // After that  fetch data 
    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            Person currentPerson = Plus.PeopleApi
                    .getCurrentPerson(mGoogleApiClient);

            String personName = currentPerson.getDisplayName();
            Log.i("personName", personName);
            ......
    }
}

参考资料:检索用户圈子中可见人员列表 希望能对您有所帮助 ツ

你仍然只得到NULL吗? - SweetWisher ツ
你是否正确地按照所有步骤进行了操作?(http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/)请确保。 - SweetWisher ツ
sh1 id会造成问题吗?因为我认为与Google控制台注册的sh1 id不同。 - Umesh Kumar Saraswat
使用SHA1生成的密钥和项目名称可以在您的项目中使用。 - SweetWisher ツ
peopleData.getStatus().getStatusCode()== 7 这是什么意思? - Umesh Kumar Saraswat
显示剩余5条评论

1
如果您正在使用Android Studio,那么这很可能是原因所在。问题出在证书上。
1)您是否使用了正确的SHA1签名来生成json配置文件和正确的包名称?
2)您是否使用相同的证书签署了应用程序?
2a)请记住,无论构建变体如何,Android Studio都将对您的应用程序进行签名。如果您没有指定一个签名,则会使用默认的开发签名。因此,除非您提供了开发证书签名来生成配置文件,否则可能会导致不匹配。因此为null。

0

这解决了我的问题,我包括了调试SHA1密钥以及发布SHA1密钥,并成功得到了返回结果。根据你是如何测试应用程序的,在"Google开发者控制台"中尝试创建一个新的客户端密钥,使用调试SHA1密钥再次尝试(这样在api控制台上会有两个客户端密钥,一个用于发布,一个用于调试)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接