Google登录片段返回RESULT_CANCELED [英] Google sign-in fragment returning RESULT_CANCELED

查看:128
本文介绍了Google登录片段返回RESULT_CANCELED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Firebase将Google登录集成到我的android应用中,但是遇到了问题.我正在此处进行教学,我相信我遵循了这个词.我将Firebase添加到我的应用中,添加了SHA-1指纹,启用了Google登录,并将依赖项添加到了成绩文件中.然后,我将代码复制到了本教程中链接到的github项目中.但是,当我运行该应用程序且代码在下面时,登录片段返回时出现错误,结果代码为RESULT_CANCELED.这是错误输出:

I am trying to integrate Google sign-in into my android app using Firebase, but I am running into issues. I am following the tutorial here, and I believe that I have followed it to the word. I added Firebase to my app, added my SHA-1 fingerprint, enabled Google sign-in, and added the dependencies to my grade files. Then, I copied the code in the github project linked to in the tutorial. However, when I run the application, and my code is below, I get an error when the sign-in fragment returns, and the result code is RESULT_CANCELED. This is the error output:

W/GoogleSignInActivity: Google sign in failed, resultCode: 0
                    com.google.android.gms.common.api.ApiException: 10: 
                        at com.google.android.gms.common.internal.zzb.zzy(Unknown Source:14)
                        at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source:37)
                        at com.example.root.firebasesignin.LoginActivity.onActivityResult(LoginActivity.java:63)
                        at android.app.Activity.dispatchActivityResult(Activity.java:7267)
                        at android.app.ActivityThread.deliverResults(ActivityThread.java:4524)
                        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4571)
                        at android.app.ActivityThread.-wrap19(Unknown Source:0)
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)
                        at android.os.Handler.dispatchMessage(Handler.java:105)
                        at android.os.Looper.loop(Looper.java:164)
                        at android.app.ActivityThread.main(ActivityThread.java:6809)
                        at java.lang.reflect.Method.invoke(Native Method)
                        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

创建SHA-1指纹时,确实必须使用命令重新创建~/.android/debug.keystore上的调试密钥库

When I created the SHA-1 fingerprint, I did have to remake the debug keystore at ~/.android/debug.keystore using the command

keytool -genkey -v -keystore ~/.android/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"

然后我使用命令将密钥库转换为pkcs12

and then I converted the keystore to pkcs12 using the command

keytool -importkeystore -srckeystore ~/.android/debug.keystore -destkeystore ~/.android/debug.keystore -deststoretype pkcs12

我从未使用过Firebase或Google登录进行身份验证,所以我很迷路.我认为密钥库可能是问题所在,当我在File > Project Structure > Signings中查看时,左侧面板中没有任何显示,并且在File > Project Structure > Build Types中,签名配置"框为空.同样,我对Firebase和Google身份验证还很陌生,因此,如果我忘记了一些简单的内容,请原谅.预先感谢您的帮助.

I have never used Firebase or Google sign-in for authentication, so I am very lost. I think the keystore might be the problem, and when I look in File > Project Structure > Signings nothing is shown in the left panel, and in File > Project Structure > Build Types the Signing Config box is empty. Again, I am very new to Firebase and Google authentication, so please excuse me if I'm forgetting something simple. Thank you in advance for your help.

这是我主要活动的代码.布局只是一个Google登录按钮和操作栏.

This is the code for my main activity. The layout is simply a Google sign-in button and the action bar.

package com.example.root.firebasesignin;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "GoogleSignInActivity";
    private static final int RC_SIGN_IN = 9001;

    private FirebaseAuth mAuth;
    private GoogleSignInClient mGoogleSignInClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // Button listeners
        findViewById(R.id.sign_in_button).setOnClickListener(this);

        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
        mAuth = FirebaseAuth.getInstance();
    }

    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = mAuth.getCurrentUser();
        updateUI(currentUser);
    }

    @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) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);
                firebaseAuthWithGoogle(account);
            } catch (ApiException e) {
                // Google Sign In failed, update UI appropriately
                Log.w(TAG, "Google sign in failed, resultCode: " + resultCode, e);
                updateUI(null);
            }
        }
    }

    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            updateUI(user);
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Toast.makeText(LoginActivity.this, "Authentiation failed", Toast.LENGTH_LONG).show();
                            updateUI(null);
                        }
                    }
                });
    }

    private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    private void updateUI(FirebaseUser user) {
        if (user != null)
            findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        else
            findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sign_in_button)
            signIn();
    }
}

推荐答案

  1. 使用Firebase开发人员帐户创建新项目.并将您的google-services.json放入您的ProjectName/App中.
  2. 然后转到Firebase开发者控制台->选择您的项目->单击项目概述(左侧)->选择您的项目设置".
  3. 转到您的应用"部分--->更新SHA证书指纹 如何获取调试器SHA1密钥?
  4. 转到android studio->单击右侧Pannel上的Gradle.
  5. 单击您的项目名称(根). --->单击任务->签署报告
  6. 在Buttom面板中单击Gradle控制台.
  7. 然后您获得了SHA1密钥.
  1. Create New Project using Firebase developer Account. And Put your google-services.json within Your ProjectName/App.
  2. Then Go to the Firebase developer console --> Select your project --> Click Project Overview(Left Side)--> Select your project Settings.
  3. Go to Your app section ---> update SHA certificate fingerprints How to get debuger SHA1 Key?
  4. Go to the android studio --> Click Gradle on the right Pannel.
  5. Click on your Project Name(root). ---> click Tasks -->signing Report
  6. Click on Gradle Console in Buttom Panel.
  7. Then you got your SHA1 key.

下一个: 1.转到Firebase开发人员控制台,然后选择您的项目. 2.单击左侧面板中的认证. 3.单击登录方法",然后将Google As true启用.

Next: 1. Go to the Firebase developer console and select your project. 2. Click Authetication in left side panel. 3. Click SIGN IN METHOD and enable Google As true.

这篇关于Google登录片段返回RESULT_CANCELED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆