Get User Name from Google Plus in android

前端 未结 5 2014
迷失自我
迷失自我 2021-01-17 16:11

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.

相关标签:
5条回答
  • 2021-01-17 16:37

    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!

    0 讨论(0)
  • 2021-01-17 16:41

    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.

    0 讨论(0)
  • 2021-01-17 16:50

    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();
    
    0 讨论(0)
  • 2021-01-17 16:51

    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();
           .........
        }
    }
    
    0 讨论(0)
  • 2021-01-17 16:56

    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.

    0 讨论(0)
提交回复
热议问题