7

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 Answers6

7

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.

Sdghasemi
  • 3,927
  • 1
  • 24
  • 32
  • Where do you find this?? I looked everywhere in the GCP Console – Zander B Sep 19 '18 at 23:21
  • @Sdghasemi Hello, I'm kinda lost here. can you help me by step by step. I really don't know how to make it. – Ticherhaz FreePalestine Sep 25 '18 at 12:39
  • @Zuhrain have you checked out the instructions I linked in my last comment? – Sdghasemi Sep 25 '18 at 23:43
  • @Sdghasemi Hi, could you tell me please, what do you mean by "using the regular debug and release api key"? Am I supposed to use some other keys? Because I'm using web application oauth id already (which is autogenerated), and it's still not working – Matvey Rybakov Dec 27 '18 at 05:40
  • 1
    @MatveyRybakov When you select Android as your application type, you also need to provide your debug and release signing-certificate fingerprint with your app package which was what I was doing. But changing the **application type** to web application made my day. Are you using web application as your _application type_? And have you checked out the instruction link I mentioned in the comments? It's the exact way you should do it to make it working. – Sdghasemi Dec 27 '18 at 07:07
  • @Sdghasemi Thanks for the answer! The application type is web application, as it supposed to be. I've tried an auto generated key which is called "Web client (Auto-created for Google Sign-in)" as well as a key I created manually (and both have web application type). I followed the guide in your link, except for step 5, which seems to be unnecessary to make it work (according to google guide). Do you use firebase btw? Could that by any chance be the root of the problem? – Matvey Rybakov Dec 27 '18 at 08:12
  • @Sdghasemi I'm also using 12.0.0 version of google auth library, which one do you use? – Matvey Rybakov Dec 27 '18 at 08:19
  • 1
    @MatveyRybakov I'm using firebase for messaging and app-indexing but for Google sign in I'm currently using `com.google.android.gms:play-services-auth:11.8.0`. – Sdghasemi Dec 27 '18 at 08:23
  • @Sdghasemi Have you also updated google-services.json file in your app folder? It seems like an unnecessary step (at least google doesn't mention it in their guide) – Matvey Rybakov Dec 27 '18 at 08:45
  • 1
    @MatveyRybakov No I didn't, but make sure you have the same client_id there compared to your server. My client_type is 3 if there would be any help. – Sdghasemi Dec 27 '18 at 09:00
  • 1
    @Sdghasemi I resolved the problem (it turned out that I was using wrong oauth id). Thanks for your help! – Matvey Rybakov Dec 27 '18 at 13:29
  • @MatveyRybakov Glad to help ;) – Sdghasemi Dec 27 '18 at 13:40
3

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.

1

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

Anga
  • 1,967
  • 2
  • 15
  • 23
1

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.

Squibly
  • 134
  • 1
  • 5
0

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

Raúl Omaña
  • 713
  • 5
  • 10
0

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

user3156040
  • 675
  • 5
  • 5