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.

  • Android In App Subscription always returns the initial receipt, I never get the renewal
  • Crashlytics don't compile when using Jack
  • Error: device offline
  • Android - drawable with rounded corners at the top only
  • Correspondence between ImageView coordinates and Bitmap Pixels - Android
  • How to set Spinner default value to null?
  • Any help will appreciated.
    Thanks.

  • Slide a layout up from bottom of screen
  • Android Maps API v2 draw circle
  • Android startService Synchronous?
  • HTTP POST request with authorization on android
  • Android missing platform tools
  • Android create layers in canvas
  • 5 Solutions collect form web for “Get User Name from Google Plus in android”

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

    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!

    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();
    

    Check that you generated both of debug and production Client ID by SHA1 keystores.
    It display in APIs & Auth -> Credentials tab of your project.

    Setting up OAuth 2.0 for Android documentation says:

    In a terminal, run the Keytool utility to get the SHA1 fingerprint for your digitally signed .apk file’s public certificate.

    …example for debug-keystore…

    Important: When you prepare to release your app to your users, follow these steps again and create a new OAuth 2.0 client ID for your production app. For production apps, use your own private key to sign the production app’s .apk file.

    I tried a lot of approaches but I was still facing the same issue. Finally, in app build.gradle file, I found that my applicationId was not same as the package name in Credentials. I fixed that and everything worked like a charm.

    Thus, make sure:

    • applicationId in build.gradle
    • package name in AndroidManifest.xml
    • package name in Credentials

    are all same.

    Hope this helps someone.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.