Unable to retrieve user information from Google+ on Android

I m Using Google+ singing in my application. when I clicked the Google Button onConnect () is called, but from the OnConnect () method I get some information from Google. but the method "Plus.PeopleApi.getCurrentPerson (mGoogleApiClient)" returns null. Please help me

// 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();
                }
            }
        }
+4
source share
3 answers

You should add this line: Plus.PeopleApi.loadVisible (mGoogleApiClient, null) .setResultCallback (this); atonConnected

Like 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);
            ......
    }
}

Link: Get a list of visible people in user circles

Hope this helps you.

+1
source

Android Studio, , , . .

1) SHA1 json ?

2) ?

2a) , Android Studio . , . , , , , . , null.

+1

This solved my problem, I enabled the debug key SHA1, as well as the release key SHA1, and I was able to return the results. Depending on how you are testing the application, in the "Google Developer Console" try creating a new client key with the SHA1 debugger and try again (so you will have 2 client keys on the api console, one for release and one for debugging).

0
source

All Articles