Google Sign in but result code is 0

Viewed 18312

I want to create a Google sign for my app but my result code from onActivityResult() is 0

In my onCreate() Method I start the function startSignInIntent() like this:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //some other stuff
        view.loadUrl(myURL);
        startSignInIntent();
}

This is the startSignInIntent()

private void startSignInIntent() {
        GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
                GoogleSignInOptions.DEFAULT_SIGN_IN);
        Intent intent = signInClient.getSignInIntent();
        startActivityForResult(intent, RC_SIGN_IN);
    }

This is the onActivityResult()

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "data : " + resultCode + " | " + data  + " || " + RC_SIGN_IN + " ||| " + requestCode);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                // The signed in account is stored in the result.
                GoogleSignInAccount signedInAccount = result.getSignInAccount();
            } else {
                String message = result.getStatus().getStatusMessage();
                if (message == null || message.isEmpty()) {
                    Toast.makeText(this, "Login failed", Toast.LENGTH_LONG).show();
                }
                new AlertDialog.Builder(this).setMessage(message)
                        .setNeutralButton(android.R.string.ok, null).show();
            }
        }

This the result of the log in the function onActivityResult()

0 | Intent { (has extras) } || 2 ||| 2

After I select an account of the the pop-up where I can choose my account. I get this screen with ok but when I have that the login already failed. See photo: enter image description here

8 Answers

Try checking if you have the correct OAuth key properly configured for your app. According to this related SO post, the OP noticed that the Auth key is also associated with other project that create the issue.

Also you can check the following implementation of Try Sign-In for Android, for code implementation:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        // The Task returned from this call is always completed, no need to attach
        // a listener.
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}

Also, some additional information provided by OP, you need to make sure you have signed the APK when you are testing it. See the documentation about Sign Your App for detailed information about it.

Hope this helps.

Make sure to set the publishing status of your OAuth consent screen to In Production status instead of Testing status.

I faced the same problem with Android Emulator. It turns out that the emulator was disconnected from the Internet that why firebase it was throwing above mentioned error.

I wanted to share my experience even though it is not applicable to the original case. My search landed here because I had the same problem - startActivityForResult returns instantly with resultCode = 0.

My cause was the wrong package name. I have an app with a few variants each of which has a different package name. Google Sign-In requires that the Android app's package name matches one of the Google API console's Android projects' package name. I set up a project for one variant, but did not for others. Once I created a project for the Google API Console, the sign-in started to work flawlessly.

In my case, I had forgotten to add the project ID to AndroidManifest.xml. Thus:

<meta-data android:name="com.google.android.gms.games.APP_ID"
           android:value="@string/app_id" />

and in the folder res/values create a file ids.xml and set the ID of the project

<string name="app_id" translatable="false">numberOfProject</string>

I had this going on specifically on android jellybean and it turned out to be that if the signin activity launch mode I was using was not standard or singletop in the manifest the onActivityResult() would return immediately without actually waiting for the result.

 <activity
       android:name=".SignInActivity"
        android:excludeFromRecents="true"
        android:exported="true"
        android:launchMode="singleTop"
        android:permission="com.google.firebase.auth.api.gms.permission.LAUNCH_FEDERATED_SIGN_IN"
        tools:replace="android:launchMode"/>

Just got it fixed... I was trying to clean up some OAuth IDs in the Credentials but removed a wrong one.

I found that this setup will create 2 OAuth 2.0 Client IDs:

  • OAuth client (Type: Android)
  • Web client (Auto-created for Google Sign-in) (Type: Web Application) <- the one I missed
Related