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.

  • Resources returns a Drawable of the wrong type
  • Scaled Bitmap maintaining aspect ratio
  • Stop or release MediaPlayer while it is still preparing
  • why android is built on a VM (Dalvik)
  • Localizing string resources added via build.gradle using “resValue”
  • How to programmatically round corners and set random background colors
  • Any help will appreciated.
    Thanks.

    Related posts:

    How can I view the shared preferences file using Android Studio?
    onDestroy gets called each time the screen goes on
    creating video from sequence of images javacv
    Android Help: How do open a remote Video file URL to play in MediaPlayer without having to open a br...
    Eclipse Juno won't create Android Activity
    Android Youtube API Available?
  • How to set a bitmap from resource
  • Android - save value on LocalStorage before WebView.loadUrl()
  • Launch App via adb without knowing Activity name
  • Call C++ library from Java in Android
  • Android studio Logcat slow in displaying results
  • Android: How to overlay-a-bitmap/draw-over a bitmap?
  • 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.