问题
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.
回答1:
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();
.........
}
}
回答2:
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!
回答3:
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();
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/22532412/get-user-name-from-google-plus-in-android