android - Android 应用程序上的 Firebase 上的 Facebook/Google 注册

标签 android facebook firebase google-authentication

第一次开发 Android 应用程序。我试图让用户在使用 FaceBook 和 Google 登录时在我们的 Firebase 中注册。

到目前为止,我已经使用电子邮件和密码登录了。 使用 Facebook 和 Google 登录。

问题是:您是否必须在 firebase 中注册用户(当从 google 和 facebook 登录时)才能创建个人资料,例如带有图片的个人资料?

代码的这个范围是在我们项目的 fragment 上实现的:

创建 View

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.google_credential))
                .requestEmail()
                .build();
        //
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .enableAutoManage(getActivity(), new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

                    }
                })
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

在 onCreateView 之外

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) {
            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", e);
                // ...
            }
        }
    }
    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        Log.d("MainACtivity", "firebaseAuthWithGoogle:" + acct.getId());

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        firebaseAuth.signInWithCredential(credential)
                .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d("Main", "signInWithCredential:onComplete:" + task.isSuccessful());

                        // 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()) {
                            Log.w("MainAcitivyt", "signInWithCredential", task.getException());
                            Toast.makeText(getActivity(), "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                        // ...
                    }
                });
    }

我按照 FireBase 指南注​​册 Google:https://firebase.google.com/docs/auth/android/google-signin

After a user signs in for the first time, a new user account is created and linked to the credentials—that is, the user name and password, phone number, or auth provider information—the user signed in with. This new account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of how the user signs in.

这是在指南之后复制的,我只是想在我的应用程序中找到我在 firebase 上注册的任何谷歌帐户。

编辑:

发现我的应用程序在使用 google 注册时出错:

这里是 logcat:

2019-03-13 11:29:25.729 10997-10997/com.itcom202.weroom W/LoginFragment: Google sign in failed
    com.google.android.gms.common.api.ApiException: 10: 
        at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source:4)
        at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source:8)
        at com.itcom202.weroom.LoginFragment.onActivityResult(LoginFragment.java:194)
        at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:160)
        at android.app.Activity.dispatchActivityResult(Activity.java:7235)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4320)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367)
        at android.app.ActivityThread.-wrap19(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        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)

最佳答案

编写 Firebase 登录身份验证时需要考虑的事项

按照他们的 Github 存储库中涵盖的流程,您可以在 Firebase Quick Start Github 中查看一些工作示例 Activity repo 。

您还需要在 Firebase 控制台中启用 Facebook 和 Google 登录。另一个重点是确保每次使用 Firebase 控制台进行更改时都使用最新的 google-services.json 文件。

最后,确保您在 Firebase 控制台中对您的 Firebase 项目进行了必要的调试和发布 SHA1 指纹设置,以便在您的应用进入 Play 商店时连接到 Firebase 服务。

访问您的 Firebase 注册用户帐户

  1. 登录到您的 Firebase 控制台。
  2. 转到您的 Firebase 项目并在左侧菜单中选择身份验证。
  3. 它将显示一个包含用户名、ID 和身份验证详细信息的用户选项卡。

有关更多信息,请查看 Firebase 文档 online .

Do you have to register the user in firebase (when sign-in from google and facebook) to be able to create a profile for example with a picture?

对于 Google Sing-In,可以使用来自 Google 帐户的个人资料图片和一些其他元数据。

如果新用户首次登录 Firebase 项目,Github Quickstart 上的 Google 登录方法和 Facebook 登录方法示例会自动添加新用户。您可以尝试添加一个新用户并在您的 Firebase 身份验证用户选项卡上看到它被添加。如下所示:enter image description here

您还可以让用户能够注销和删除他们的登录帐户,文档和 Github 示例中也有介绍。

关于android - Android 应用程序上的 Firebase 上的 Facebook/Google 注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55139298/

相关文章:

iphone - 在 Facebook 墙和 Facebook friend 墙上张贴图片

android - 火力地堡存储 "error sending network request GET"

node.js - 使用 async/await 在 firebase 中运行查询

android - 按钮边距的布局问题

java - 当我返回 Activity 时,如何重新启动 BarcodeDetector 以获得新结果?

java - Android:似乎无法正确使用 MotionEvent.ACTION_MOVE

facebook - 使用 Facebook Graph API v3.0 列出群组事件

android - Facebook 登录 Android : This app has no key hashes configured

android - 应用程序崩溃 - Web 服务没有返回

ios - 尝试显示身份验证选择器时 FirebaseUI 崩溃