问题
I am trying to include Google sign in in my android application using Google+ Api. I am able to take account details from the user but once signed in I am getting null when requesting for username using call:
Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getDisplayName()
And Logcat shows:
BasicNetwork.performRequest: Unexpected response code 403 for https://www.googleapis.com/plus/v1/people/me
Although I am able to get email of the user using:
Plus.AccountApi.getAccountName(GoogleClient.mGoogleApiClient)
Please help me to discover my mistake
回答1:
I have tried same code and its working fine! So, just ensure two things:
1)Register your digitally signed .apk file's public certificate in the Google APIs Console.Link below:
https://developers.google.com/+/mobile/android/getting-started#step_1_enable_the_google_api
2)Make sure you have added the google+ api access and have client key created with SHA1.
Rest is fine.
回答2:
I think basic steps of google+ login integration in app is known by you.Here are the rest of the steps
step 1-- in oncreate
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
step 2-- in login button click listener call this----
private void LoginGoogle(){
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
if (errorCode != ConnectionResult.SUCCESS) {
GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
}
else{
//perform login
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
mGoogleApiClient.connect();
}
}
}
step 3---- in onActivityresult
if (requestCode == RC_SIGN_IN) {
if (resultCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.reconnect();
}
}
step 4-----
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
if(!mIntentInProgress){
if ( mSignInClicked && result.hasResolution()) {
// The user has already clicked 'sign-in' so we attempt to resolve all
// errors until the user is signed in, or they cancel.
try {
result.startResolutionForResult(this, RC_SIGN_IN);
mIntentInProgress = true;
} catch (SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
mSignInClicked = false;
getProfileInformation();
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
step 5-----
private void getProfileInformation(){
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String id=currentPerson.getId();
String personName = currentPerson.getDisplayName();
String personPhoto = currentPerson.getImage().getUrl();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
String profilePic=personPhoto.substring(0,
personPhoto.length() - 2)
+ ProfilePicSize;
Log.e("GOOGLE", id);
Log.e("GOOGLE", personName);
Log.e("GOOGLE", profilePic);
Log.e("GOOGLE",email);
}
} catch (Exception e) {
e.printStackTrace();
}
}
来源:https://stackoverflow.com/questions/30843593/getting-user-credentials-using-google-api