Retrieving user credentials using the Google+ API
I am trying to enable google sign in in android app using Google+ Api. ... I can get the account details from the user, but after logging in, I get null when prompted for the username using the call:
Plus.PeopleApi.getCurrentPerson (mGoogleApiClient) .getDisplayName ()
And Logcat shows:
BasicNetwork.performRequest: Unexpected 403 response code for https://www.googleapis.com/plus/v1/people/me
Although I can get the user's email using:
Plus.AccountApi.getAccountName (GoogleClient.mGoogleApiClient)
Please help me find my error.
source to share
I tried the same code and it works great! So, just provide two things:
1) Register your public .apk open source certificate in the Google API Console. Below is the list:
https://developers.google.com/+/mobile/android/getting-started#step_1_enable_the_google_api
2) Make sure you add google + api access and create client key with SHA1.
Rest is fine.
source to share
I think the basic steps of integrating google + app login are familiar to 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 the login window, click the listen button to trigger 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();
}
}
source to share