I followed the official guide about google log-in on Android device.
I succesfully get the Account picker
but when I handle the result, it is always not successful.
This is what i've done:
- I logged on my console, I set up the
SHA-1
for this project obtained with the console command on the keystore I used on for creating my apk. (newly created). - Then I downloaded the google-services.json file and I put it into my app/ folder.
- Finally I implemented the login as suggested on the guide (posting code below)
Fragment with sign in button
public class SigninMenuFragment extends Fragment implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private final static String TAG = "SigninMenuFragment";
private Button btnFacebookLogin, btnSignin, btnSignup;
private Button btnGoogleLogin;
private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
private ProgressDialog mProgressDialog;
public SigninMenuFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_signin_menu, container, false);
btnFacebookLogin = (Button) view.findViewById(R.id.btnLoginFacebook);
btnGoogleLogin = (Button) view.findViewById(R.id.btnLoginGoogle);
btnSignin = (Button) view.findViewById(R.id.btnSignIn);
btnSignup = (Button) view.findViewById(R.id.btnSignUp);
btnGoogleLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
googleLogin();
}
});
btnFacebookLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
facebookLogin();
}
});
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(getContext())
.enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
return view;
}
private void facebookLogin() {
//todo
}
private void googleLogin() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
GoogleSignInAccount a = result.getSignInAccount();
if(a == null){
Log.d(TAG, "null");
}else{
Log.d(TAG, a.getDisplayName());
}
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
} else {
// Signed out, show unauthenticated UI.
}
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
}
private void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("Loading");
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
private void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.hide();
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.d(TAG, "Connected");
}
@Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "NOT Connected");
}
}
My manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<application
android:name=".other.MyCustomApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activities.SplashScreenActivity"
android:theme="@style/AppThemeNoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- ... -->
</application>
Gradle
//...
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.code.gson:gson:2.7'
compile 'com.android.support:multidex:1.0.1'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.google.android.gms:play-services-places:11.4.0'
compile 'com.google.android.gms:play-services-location:11.4.0'
compile 'com.google.android.gms:play-services-maps:11.4.0'
compile 'io.realm:android-adapters:2.1.0'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.google.firebase:firebase-core:11.4.0'
compile 'com.google.firebase:firebase-messaging:11.4.0'
compile 'com.facebook.android:facebook-login:[4,5)'
compile 'com.google.android.gms:play-services-auth:11.4.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
Some annotations:
- My
activity
extends theFragmentActivity
- The login
button
is custom
My biggest doubt is about the SHA-1 generation, since the other files are copy-pasted from google dev console.
I need the full-name of the account owner and the base user information
Any idea of what I'm doing wrong?
Thanks all
EDIT
LogCat when I click the button
10-19 09:32:40.974 23319-23319/utilitapps.prodottilastminute I/HwSecImmHelper: mSecurityInputMethodService is null 10-19 09:32:41.048 23319-23319/utilitapps.prodottilastminute V/AudioManager: playSoundEffect effectType: 0 10-19 09:32:41.049 23319-23319/utilitapps.prodottilastminute V/AudioManager: querySoundEffectsEnabled...
--------- beginning of system 10-19 09:32:41.068 23319-23483/utilitapps.prodottilastminute V/FA: Recording user
engagement, ms: 15514 10-19 09:32:41.069 23319-23483/utilitapps.prodottilastminute V/FA: Using measurement service 10-19 09:32:41.070 23319-23483/utilitapps.prodottilastminute V/FA: Connecting to remote service 10-19 09:32:41.082 23319-23483/utilitapps.prodottilastminute V/FA: Activity paused, time: 144716338 10-19 09:32:41.086 23319-23319/utilitapps.prodottilastminute V/ActivityThread: ActivityThread,callActivityOnCreate 10-19 09:32:41.087 23319-23319/utilitapps.prodottilastminute V/FA: onActivityCreated 10-19 09:32:41.087 23319-23483/utilitapps.prodottilastminute D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=15514, firebase_screen_class(_sc)=SplashScreenActivity, firebase_screen_id(_si)=2950491777079955672}] 10-19 09:32:41.122 23319-23483/utilitapps.prodottilastminute V/FA: Using measurement service 10-19 09:32:41.122 23319-23483/utilitapps.prodottilastminute V/FA: Connection attempt already in progress 10-19 09:32:41.124 23319-23483/utilitapps.prodottilastminute V/FA: Using measurement service 10-19 09:32:41.124 23319-23483/utilitapps.prodottilastminute V/FA: Connection attempt already in progress 10-19 09:32:41.124 23319-23483/utilitapps.prodottilastminute V/FA: Activity resumed, time: 144716382 10-19 09:32:41.128 23319-23483/utilitapps.prodottilastminute D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=SplashScreenActivity, firebase_previous_id(_pi)=2950491777079955672, firebase_screen_class(_sc)=SignInHubActivity, firebase_screen_id(_si)=2950491777079955674}] 10-19 09:32:41.163 23319-23483/utilitapps.prodottilastminute V/FA: Using measurement service 10-19 09:32:41.164 23319-23483/utilitapps.prodottilastminute V/FA: Connection attempt already in progress 10-19 09:32:41.169 23319-23483/utilitapps.prodottilastminute V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 57 10-19 09:32:41.169 23319-23483/utilitapps.prodottilastminute V/FA: Using measurement service 10-19 09:32:41.169 23319-23483/utilitapps.prodottilastminute V/FA: Connection attempt already in progress 10-19 09:32:41.169 23319-23483/utilitapps.prodottilastminute V/FA: Activity paused, time: 144716439 10-19 09:32:41.196 23319-23483/utilitapps.prodottilastminute D/FA: Connected to remote service 10-19 09:32:41.196 23319-23483/utilitapps.prodottilastminute V/FA: Processing queued up service tasks: 5 10-19 09:32:46.263 23319-23483/utilitapps.prodottilastminute V/FA: Inactivity, disconnecting from the service
LogCat when I select the account:
10-19 09:34:49.570 23319-23319/utilitapps.prodottilastminute I/HwSecImmHelper: mSecurityInputMethodService is null 10-19 09:34:49.583 23319-23319/utilitapps.prodottilastminute D/SigninMenuFragment: handleSignInResult:false 10-19 09:34:49.583 23319-23319/utilitapps.prodottilastminute D/SigninMenuFragment: null 10-19 09:34:49.585 23319-25806/utilitapps.prodottilastminute V/FA: Activity resumed, time: 144844854 10-19 09:34:49.592 23319-25806/utilitapps.prodottilastminute D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=SignInHubActivity, firebase_previous_id(_pi)=2950491777079955675, firebase_screen_class(_sc)=SplashScreenActivity, firebase_screen_id(_si)=2950491777079955672}] 10-19 09:34:49.598 23319-23319/utilitapps.prodottilastminute I/HwSecImmHelper: mSecurityInputMethodService is null