Getting User Credentials Using Google+ Api
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 request
Solution 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.
Solution 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----
privatevoidLoginGoogle(){
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
if (errorCode != ConnectionResult.SUCCESS) {
GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
}
else{
//perform loginif (!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-----
@OverridepublicvoidonConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stubif(!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();
}
}
}
}
@OverridepublicvoidonConnected(Bundle arg0) {
// TODO Auto-generated method stub
mSignInClicked = false;
getProfileInformation();
}
@OverridepublicvoidonConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
mGoogleApiClient.connect();
}
@OverrideprotectedvoidonStop() {
// TODO Auto-generated method stubsuper.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
step 5-----
privatevoidgetProfileInformation(){
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();
}
}
Post a Comment for "Getting User Credentials Using Google+ Api"