问题
I have an Android app for a customer, who wishes the users be able to log in via Google. So far, I have implemented the Google SignInButton, the GoogleSignInOptions, and the GoogleApiClient.
However, when I try to log in with:
googleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_REQUIRED);
the application force quits as soon as it is opened with following exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.nwt.slottynative, PID: 24509
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.nwt.slottynative/de.nwt.slottynative.FullscreenActivity}: java.lang.IllegalStateException: Cannot use sign-in mode: SIGN_IN_MODE_REQUIRED. Mode was already set to SIGN_IN_MODE_NONE
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2739)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2804)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6066)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.IllegalStateException: Cannot use sign-in mode: SIGN_IN_MODE_REQUIRED. Mode was already set to SIGN_IN_MODE_NONE
at com.google.android.gms.internal.zzmg.zzbC(Unknown Source)
at com.google.android.gms.internal.zzmg.connect(Unknown Source)
at de.nwt.slottynative.FullscreenActivity.onCreate(FullscreenActivity.java:239)
at android.app.Activity.performCreate(Activity.java:6368)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2692)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2804)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6066)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
If I try to log in with:
googleApiClient.connect();
I am redirected to the account selection fragment, however, the login fails:
03-17 15:30:53.005 27135-27135/de.nwt.slottynative D/handleSignInResult: handleSignInResult: false
03-17 15:30:53.015 27135-27135/de.nwt.slottynative D/handleSignInResult: status: Status{statusCode=unknown status code: 12501, resolution=null}
Here is the code which initializes the API:
//<editor-fold desc="Google SDK Init" >
googleSignInOps = new GoogleSignInOptions.Builder().requestEmail().requestId().requestProfile()
.requestIdToken("1042722069660-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com").build();
googleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
onGoogleConnectionFailed(connectionResult);
}
}).addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOps).build();
googleApiClient.connect();
googleLoginButton = (SignInButton)findViewById(R.id.googleLogin);
googleLoginButton.setSize(SignInButton.SIZE_WIDE);
googleLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == googleLoginButton.getId())
signIn();
}
});
//</editor-fold>
Thanks in advance for your help!
回答1:
If you're using the SignIn API you should connect with the mode - GoogleApiClient.SIGN_IN_MODE_OPTIONAL:
googleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL)
Note that if permissions and scopes you have requested through the SignInOptions are not granted by the user the client will still connect successfuly.
来源:https://stackoverflow.com/questions/36063607/google-signin-not-working