3

I can sign in my app with Google account at first several times. Everything is fine.

But if I sign in and out about 20 times in a one or two minutes. Google sign in failed and in onActivityResult function, it returns error code 12501, resultCode = 0;

I'm using the phone: Nexus 6, Android 5.1.1

Here is my code:

private GoogleSignInOptions mGso;
private GoogleApiClient mGac;

public void init(@NonNull final BaseActivity activity) {
    mGso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(activity.getString(R.string.default_web_client_id))
            .requestEmail()
            .build();

    mGac = new GoogleApiClient.Builder(activity)
            .enableAutoManage(activity /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    ToastUtils.show(activity, R.string.login_failed);
                }
            })
            .addApi(Auth.GOOGLE_SIGN_IN_API, mGso)
            .build();
}

public void signIn(@NonNull final BaseActivity activity,
                   @NonNull GoogleSignInCallback callback,
                   @NonNull final OnLoadingListener<PlatformUserEntity> listener) {

    callback.registerCallback(listener);
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGac);
    activity.startActivityForResult(signInIntent, REQUEST_GOOGLE_SIGNIN);

    // disconnect the client
    mGac.stopAutoManage(activity);
    mGac.disconnect();

}

Here is gradle:

compile 'com.google.android.gms:play-services-base:9.6.1'
compile 'com.google.android.gms:play-services-gcm:9.6.1'
compile 'com.google.android.gms:play-services-auth:9.6.1'

Fisrt, I init the GoogleApiClient with a FragmentActivity, then signIn function starts the Acitvity. GoogleSignInCallback is registered in the onActivityResult function. Then disconnect the Client because every time the sign in button is clicked, the init function will be invoked.

I doubt that I use stopAutoManage() too early but it seems like not true. So I'm confused, which part might be wrong?

I noticed the log:

Could not set socket write timeout: null
12-03 17:21:43.859 264-264/? W/SurfaceFlinger: couldn't log to binary event log: overflow.
12-03 17:21:43.902 1946-12870/? W/Conscrypt: Could not set socket write timeout: null
12-03 17:21:44.327 21168-21168/? W/AccountChipsFragment: Recording consent failed.
12-03 17:21:44.657 29359-29782/? E/TokenRequestor: You have wrong OAuth2 related configurations, please check. Detailed error: UNREGISTERED_ON_API_CONSOLE
12-03 17:21:44.664 812-1072/? W/ActivityManager: getRunningAppProcesses: caller 10145 does not hold REAL_GET_TASKS; limiting output
12-03 17:21:44.697 21168-21168/? W/AutoManageHelper: Unresolved error while connecting client. Stopping auto-manage.

It said "You have wrong OAuth2 related configuration", but I could use the web client id to request the IdToken at the first time.

It just makes me more confused.

I also found a strange thing. If I install the apk which is built locally, this error never happened. If I download from google play store, this error ocurred. But there is no difference between these two apks because I deliver google store with the local one.

chevy1006
  • 191
  • 3
  • 10

3 Answers3

11

Finally I found the reason. My apk was signed again by our company's release procedure. The procedure used another keystore so my sha1 key was changed. I configure the new sha1 key in google develop console, this bug is solved.

But I'm still confused that, if I use the debug keystore apk, got sign in successfully and uninstall it, then I install the google play apk which has different sha1 key, google sign in can work some times.It won't tell me wrong immediately.

chevy1006
  • 191
  • 3
  • 10
  • This could be a rookie question, but could you describe where you can configure the sha1 key in the google dev console? – Xavier Portebois Dec 06 '16 at 16:08
  • @XavierPortebois Yes, I have no idea about the release procedure by our company. I use firebase cloud messaging, the sha1 key is set in the console of [firebase](console.firebase.google.com). Or you can configure it at [here](https://developers.google.com/mobile/add?platform=android) if you don't need firebase. – chevy1006 Dec 07 '16 at 03:12
  • I found that after a little struggle. I never read in the "sign your app" doc that you have to register the signed APK through the dev console, but anyway, now it's done. For people with the same problem, if you're still stuck, you could check my own step-by-step, maybe it could help: http://stackoverflow.com/a/41012703/923712 – Xavier Portebois Dec 07 '16 at 08:37
0

if your code working well at first time then you can try that way,

private GoogleSignInOptions mGso;
private GoogleApiClient mGac;

public void signIn(@NonNull final BaseActivity activity,
           @NonNull GoogleSignInCallback callback,
           @NonNull final OnLoadingListener<PlatformUserEntity> listener) {

    mGso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(activity.getString(R.string.default_web_client_id))
            .requestEmail()
            .build();

    mGac = new GoogleApiClient.Builder(activity)
            .enableAutoManage(activity /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    ToastUtils.show(activity, R.string.login_failed);
                }
            })
            .addApi(Auth.GOOGLE_SIGN_IN_API, mGso)
            .build();

    callback.registerCallback(listener);
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGac);
    activity.startActivityForResult(signInIntent, REQUEST_GOOGLE_SIGNIN);

    // disconnect the client
    mGac.stopAutoManage(activity);
    mGac.disconnect();

}

or as your log mention:

 You have wrong OAuth2 related configurations, please check. Detailed error: UNREGISTERED_ON_API_CONSOLE

So, Problem can be:
    1. Sh1 key add in your api
    2. Api key type like for android or web
    3. Check Internet Connection

Sorry, for my english. I hope this will help you. thanks

  • 1
    I also found a strange thing. If I install the apk which is built locally, this error never happened.If I download from google play store, this error ocurred.But there is no difference between these two apks because I deliver google store with the local one. – chevy1006 Dec 04 '16 at 09:56
  • You were right, my SHA1 key got wrong by some reason.[here is the reason](http://stackoverflow.com/a/40988326/7244483) – chevy1006 Dec 06 '16 at 05:20
0

Try the solution in this SO question, by changing your code and using this code

mGso = newGoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(AuthenticatedActivity.this.getResources()
.getString(R.string.server_client_id))
.requestEmail()
.build();

For more information, check this another SO question if it can help you.

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31