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

问题描述 投票:5回答:4

我试着学习如何在我的Android应用程序中使用谷歌登录,但我抓住com.google.android.gms.common.api.ApiException:16我找不到stackoveflow的答案,它是什么,为什么我抓住它。在我阅读的文档中,它“被用户取消”,但我的谷歌帐户被接受安装应用程序

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()
        }

    }

}

我跟着this guide。做了项目的配置。如果这很重要,我会使用VDS。帐户是在同一个地方创建的

这是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)
android google-signin googlesigninaccount
4个回答
6
投票

我遇到了同样的问题,启动活动结果继续使用RESULT_CANCELED和errorCode 16.问题是我在Google Cloud Platform Console中的客户端配置。我正在使用常规的调试和发布api密钥。当我使用Web应用程序作为我的Google控制台配置时,结果返回OK

希望能帮助到你。


1
投票

有同样的问题,结果我没有在firebase项目设置上设置支持邮件。

如果是这种情况,firebase将在您尝试启用Google登录Firebase身份验证时显示编辑项目设置。您可以从firebase复制客户端ID


0
投票

也许这里的派对有点晚了,但经过4个多小时的调试,我意识到:

1.-在OAuth客户端ID列表下添加带有签名证书指纹的Android客户端。这是强制性的。

2.-在您的代码中添加Web应用程序客户端ID,以防您需要获取id令牌

// 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();

希望能帮助到你


0
投票

对我来说唯一有用的是提供2个oauth客户端ID。 Web应用程序客户端ID和Android客户端ID

在我的Android应用程序中,我使用Web应用程序的客户端ID和客户端密钥。即使我不在我的应用程序中的任何地方使用Android客户端ID它仍然是必需的。即如果我删除谷歌api控制台中的oauth android客户端我的应用程序停止工作即使我不在我的应用程序任何地方使用该客户端ID。

这对我来说绝对没有意义!去搞清楚 。但到目前为止,这是唯一有效的方法。

未吓坏-可信。

© www.soinside.com 2019 - 2024. All rights reserved.