-->

Android Google Auth Sign In get Id token handleSig

2019-02-25 05:54发布

问题:

I am setting GoogleSignInOptions and Google Api Client like this

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.server_client_ID))
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .addApi(Plus.API)
            .build();

and my google web app client id like this:

 1020847812450-xxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com

but always at onActivityResult

    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSignInResult(result);
    }

is returning false

where am i doing wrong here :S

onStart Section

    mGoogleApiClient.connect();
    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.
        Log.d(TAG, "Got cached sign-in");
        // 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.

        opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
            @Override
            public void onResult(GoogleSignInResult googleSignInResult) {

                handleSignInResult(googleSignInResult);
            }
        });
    }

onStop section

protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

onHandleSignInResult

private void handleSignInResult(GoogleSignInResult result) {
    Log.e(TAG, "handleSignInResult:" + result.isSuccess());
    if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        final GoogleSignInAccount acct = result.getSignInAccount();
        Log.e(TAG, acct.getDisplayName());

    }
}

回答1:

I am also facing the same issue.First remove the current OAuth client ID,after that create one more OAuth client ID.Its worked for me.



回答2:

Are you getting error 12501? I also had this issue because I was using debug.keystore which comes with the SDK (for some reason unknown to me, it didn't work). I created a new one on my own, got SHA-1 hash from it, entered in Google API console and then it worked.

Be sure you set up signing configs for both debug and release builds with the new keystore.



回答3:

Follow all the step!!..

Release APK and debug APK has different SHA1 and different API keys for google services. Both of them must be added in Firebase Console -> Project settings. Then download google-services.json from here, add it to project and recompile with release keystore using the option "Build signed APK". That should work

and also read carefully...

https://developer.android.com/studio/publish/app-signing



回答4:

I believe that you need a call to client.connect(); as per documentation example:

GoogleApiClient client = new GoogleApiClient.Builder(this)
         .addApi(Plus.API)
         .addScope(Plus.SCOPE_PLUS_LOGIN)
         .setAccountName("users.account.name@gmail.com")
         .build();
 client.connect();

Or is it missing from your question and you are calling connect somwhere else in your code?