Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get User Name from Google Plus in android

I have integrated Google plus with my android app. Everything is working fine, i am also connected to Google plus but I am not able to get the name of current user logged.

public void onConnected(Bundle connectionHint) {
    String personName="Unknown";
    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
        Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
        personName = currentPerson.getDisplayName();
    }
}

Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) this method always return null.

Any help will appreciated. Thanks.

like image 759
user3441778 Avatar asked Mar 20 '14 12:03

user3441778


3 Answers

For me the cause of this call returning null was that the Google+ API Was not enabled for my application. Navigate to https://console.developers.google.com, select your project and enable the Google+ API to get it working!

like image 50
Peter Avatar answered Oct 18 '22 00:10

Peter


You have to add this line:

Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);

Like this:

public void onConnected(Bundle connectionHint) {

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

    String personName="Unknown";
    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
       Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
       personName = currentPerson.getDisplayName();
       .........
    }
}
like image 27
trinisites Avatar answered Oct 17 '22 23:10

trinisites


In my case the null returned because I have added that line:

addScope(new Scope(Scopes.EMAIL))

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(new Scope(Scopes.PROFILE))
            //.addScope(new Scope(Scopes.EMAIL))
            .build();

================= UPDATE ======================== Scopes should be set as below:

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .build();
like image 5
Alexey Avatar answered Oct 17 '22 23:10

Alexey