Google sign in failed after frequent signing in and out

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

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.

回答1:

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.



回答2:

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



回答3:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!