I have used Google sign-in feature in my android app. I have used their
standard code for login and have tested it by deploying it on my mobile
device in debug mode. Everything works fine this way.
I have now moved my app to Google Play Store for closed beta testing. When
I install my app from Play Store it fails at the login step as I noticed it
returns null for user's email, id and displayName fields. I have no idea
what is going wrong after installing it from Play Store.
The sign-in code is given below:
private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
private GoogleSignInAccount acct;
GoogleSignInOptions gso = new
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /*
OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
SignInButton signInButton = (SignInButton)
findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setScopes(gso.getScopeArray());
@Override
public void onStart() {
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr =
Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the
OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
isFirstLogin = false;
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the
sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently.
Cross-device
// single sign-on will occur in this branch.
isFirstLogin = true;
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from
GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result =
Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
// [END onActivityResult]
// [START handleSignInResult]
private void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
acct = result.getSignInAccount();
mStatusTextView.setText(getString(R.string.signed_in_fmt,
acct.getDisplayName()));
updateUI(true);
final AsyncTask<Integer, Void, HelloUserResponse> addUser =
new AsyncTask<Integer, Void, HelloUserResponse>()
{
@Override
protected HelloUserResponse doInBackground(Integer... integers) {
// Retrieve service handle.
Helloworld apiServiceHandle = AppConstants.getApiServiceHandle();
try {
HelloUser heUser = new HelloUser();
heUser.setUseremail(acct.getEmail());
heUser.setUserid(acct.getId());
heUser.setUsername(acct.getDisplayName());
Helloworld.Greetings.AddUser addUserCommand =
apiServiceHandle.greetings().addUser(heUser);
HelloUserResponse helloUserRsp = addUserCommand.execute();
return helloUserRsp;
} catch (IOException e) {
Toast.makeText(SignInActivity.this, "Something went wrong",
Toast.LENGTH_SHORT).show();
}
return null;
}
@Override
protected void onPostExecute(HelloUserResponse helloUserRsp) {
if(helloUserRsp.getResult()!=0L)
{
Intent myIntent = new Intent(getApplicationContext(),
MainActivity.class);
SharedPreferences myprefs =
getApplicationContext().getSharedPreferences("user", MODE_WORLD_READABLE);
myprefs.edit().putString("user_name", acct.getDisplayName()).commit();
myprefs.edit().putString("user_email", acct.getEmail()).commit();
myprefs.edit().putLong("user_id", helloUserRsp.getResult()).commit();
startActivity(myIntent);
}
else {
updateUI(false);
}
}
};
if(isFirstLogin)
addUser.execute(0);
else
{
Intent myIntent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(myIntent);
}
}
else {
// Signed out, show unauthenticated UI.
updateUI(false);
}
}
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/499de814-3121-434c-98d9-dda409d857f2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.