com.google.android.gms.common.api.ApiException: 16:

13.8k Views Asked by At

I try to learn how i can use google sign in in my android App, but i catch com.google.android.gms.common.api.ApiException: 16 And i can't find on stackoveflow answer, what is it and why i catch it. In documentation i read, what it "was canceled by user", but my google account accepted to install apps

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import android.content.Intent
import com.google.android.gms.tasks.Task
import com.google.android.gms.common.api.ApiException

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build()

        val mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
        val account = GoogleSignIn.getLastSignedInAccount(this)
        if(account != null){
            Log.e("!!!", account.email)
        } else {
            val signInIntent = mGoogleSignInClient.signInIntent
            startActivityForResult(signInIntent, 0)
        }
    }

    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

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

    private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
        try {
            val account = completedTask.getResult(ApiException::class.java)

            // Signed in successfully, show authenticated UI.
            Log.e("!!!", account.email)
        } catch (e: ApiException) {
            e.printStackTrace()
        }

    }

}

I followed this guide. Did the configuration of the project. If it's matter, i use VDS for this. Account was created in the same place

Here is stackTrace:

com.google.android.gms.common.api.ApiException: 16: 
    at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source)
    at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source)
    at foryou.friendly.alisa.alisa.MainActivity.onActivityResult(MainActivity.kt:47)
    at android.app.Activity.dispatchActivityResult(Activity.java:7124)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4173)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4220)
    at android.app.ActivityThread.-wrap20(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1579)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6228)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
6

There are 6 best solutions below

12
Sdghasemi On BEST ANSWER

I had the same problem, start activity result kept coming back with RESULT_CANCELED and errorCode 16. The problem was my client configuration in Google Cloud Platform Console. I was using the regular debug and release api key. The result came back OK when I used web application as my Google Console configuration.

Hope it helps.

0
Raúl Omaña On

Maybe a bit late to the party here but after more than 4 hours debugging I realized that:

1.- Add an Android client with your signing-certificates fingerprints under the OAuth client IDs list. This is mandatory.

2.- Add the Web application client ID in your code in case your need to get an id token

// ID and basic profile are included in DEFAULT_SIGN_IN
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                            .requestIdToken("YOUR_CLIENT_ID")
                            .requestEmail()
                            .build();

Hope it helps

0
Anga On

Was having the same problem, turns out I did not set support mail on firebase project settings.

If this is the case, firebase will show you edit project settings when trying to enable Google sign in on Firebase Authentication. You can copy your client Id from firebase

0
Squibly On

For me the only thing that worked was to provide 2 oauth client ids. a web application client id AND an android client id

In my android app i use the client id and client secret of the web application. Even though i don't use the android client id anywhere in my app it is still required. ie if i delete the oauth android client in the google api console my app stops working EVEN THOUGH i dont use that client id ANYWHERE in my app.

this makes absolutely no sense to me! go figure . but so far this is the only thing that has worked.

un-freaking-believable.

0
Dimitris Paxinos On

I am developing an Android application using Flutter, tried to integrate Google Sign In and faced the same problem with ApiException: 16 and SIGN_IN_FAILED (instead of RESULT_CANCELED).

Application type on Firebase was set to android.

In my case, after many hours of debugging, it turned out to be a wrong SHA-1 problem.

As soon as I extracted the SHA-1 key from my project and updated Firebase console, it worked.

0
user3156040 On

I used a different oauth id key which i found from downloading the project settings and added the client 3 key in the project.