Google登录身份验证无效

时间:2016-08-01 21:34:31

标签: java android firebase google-api firebase-authentication

我使用过Stackoverflow并尝试了建议的答案,但没有一个正在运行。我试图用firebease和谷歌授权用户,我的代码工作正常。但在这种方法中,我总是得到toast消息身份验证失败:

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

    // Build a GoogleApiClient with access to the Google Sign-In API and the
    // options specified by gso.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

我认为它与我的google-services.json和我的谷歌API凭据有关。我想知道是否有人可以请给我一些明确的步骤来解决这个问题。这就是我尝试使登录过程有效的方法。

  1. 将我的应用程序与firebase链接,并将JSON正确添加到项目中。
  2. 添加了所有构建gradle依赖项
  3. 允许Google从firebase身份验证标签登录
  4. 去谷歌API凭证网站(here)并将其复制并存储在我项目内的strings.xml中
  5. 返回凭据页面并为Android创建另一个Auth令牌,并将其输入终端以获取ShA1密钥:
  6.   

    keystore -list -vrt -alias androiddebugkey -keystore~ / .android / debug。

    密码为android

    然后我在创建android Auth令牌时输入了这个键。

    我还在onCreate

    中的signin类中包含了所有这些代码
    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                Toast.makeText(LoginActivity.this, "Passed google login", Toast.LENGTH_SHORT).show();
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = result.getSignInAccount();
    
                firebaseAuthWithGoogle(account);
            }
        }
    }
    
    /*****************************************************************************************************/
    /*get an ID token from the GoogleSignInAccount object, exchange it for a Firebase credential,
     *and authenticate with Firebase using the Firebase credential
     */
    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    
        auth.signInWithCredential(credential)
                .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        // If sign in fails, display a message to the user. If sign in succeeds
                        // the auth state listener will be notified and logic to handle the
                        // signed in user can be handled in the listener.
                        if (!task.isSuccessful()) {
                            Toast.makeText(LoginActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                        // ...
                    }
                });
    }
    

    以及所有这些,以及监听器和onstart以及停止方法

    Magic Close feature:
    
    If a driver supports "Magic Close", the driver will not disable the
    watchdog unless a specific magic character 'V' has been sent to
    /dev/watchdog just before closing the file.  If the userspace daemon
    closes the file without sending this special character, the driver
    will assume that the daemon (and userspace in general) died, and will
    stop pinging the watchdog without disabling it first.  This will then
    cause a reboot if the watchdog is not re-opened in sufficient time.
    

    我已经被困在这一段很长一段时间了,非常感谢任何帮助。我不清楚我的问题是什么,我只是猜测。如果需要任何进一步的信息,请告诉我,我将更新这篇文章。

    感谢

1 个答案:

答案 0 :(得分:2)

这是一系列建议而不是答案。

更新完成侦听器以记录失败的异常:

                    if (!task.isSuccessful()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(GoogleSignInActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }

运行时,查看logcat输出以查看与auth处理相关的异常和其他日志消息。

我想知道您发布的这一步以及与项目的SHA1指纹相关的步骤顺序:

  

步骤4.去谷歌API凭证网站...

我已经在一些项目中进行了身份验证,包括Auth QuickStart,并且不记得需要使用API​​凭据网站。应在Firebase console的项目的“设置”页面中输入SHA1密钥。您应该在之前下载google-service.json文件,以便将其包含在文件中。通过查看certificate_hash此部分的值,您可以查看您使用的文件是否具有正确的指纹:

  "oauth_client": [
    {
      "client_id": "<long ID string here>",
      "client_type": 1,
      "android_info": {
        "package_name": "com.google.firebase.quickstart.auth",
        "certificate_hash": "<your SHA1 fingerprint here>"
      }
    },