【问题标题】:Unable to get Google OAuth authentication working with React Native and Firebase无法使用 React Native 和 Firebase 获得 Google OAuth 身份验证
【发布时间】:2016-03-08 20:49:23
【问题描述】:

我正在尝试让 Google OAuth 与 Firebase 和 React Native 一起使用,但似乎无法让所有东西一起工作。我正在使用 NPM 包 react-native-google-signin 来处理 Google OAuth 方面的事情,并且按预期工作。但是,我在让用户通过 Firebase 进行身份验证时遇到了问题。

我已按照网络指南中的说明在我的 Firebase 实例上启用了 Google 身份验证。但是,当我尝试使用本机 OAuth 包中的 idToken 字段进行身份验证时,我总是从 Firebase 收到 INVALID_CREDENTIALS 错误。我正在使用 Firebase Web API 中的 authWithOAuthToken 方法。

我想我已经尝试了将 Web/Android 客户端 ID 用于 Firebase 配置以及 OAuth 包中的 idTokenserverAuthCode 字段的所有组合,但一切都以相同的错误结束。有没有人让它正常工作?

【问题讨论】:

  • 你有什么消息吗?

标签: android firebase react-native google-oauth firebase-authentication


【解决方案1】:

这就是我为我的应用程序所做的:

import {GoogleSignin} from 'react-native-google-signin';

const KEYS = {
    // ... // ios and web keys (i'm loading them from my server)
} 

login() {
  GoogleSignin.hasPlayServices({ autoResolve: true })
   .then(() => {
      GoogleSignin.configure(KEYS)
        .then(() => {
          GoogleSignin.signIn()
            .then(this.onGoogleLoginSuccess)
            .catch(error=>{})
            .done();
        });
   })
   .catch((err) => {
     console.log("Play services error", err.code, err.message);
   })
}

onGoogleLoginSuccess(user) {
  var token = user.idToken;
  var provider = firebase.auth.GoogleAuthProvider;
  var credential = provider.credential(token);
  firebase.auth().signInWithCredential(credential)
    .then((data)=>console.log('SUCCESS', data))
    .catch((error)=>console.log('ERROR', error));
}

重点是使用signInWithCredential()方法。参考:https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithCredential

【讨论】:

  • 你能告诉.then((data)=>console.log('SUCCESS', data))上应该打印什么吗?
猜你喜欢
  • 1970-01-01
  • 2017-07-12
  • 2023-02-14
  • 1970-01-01
  • 2021-10-31
  • 2018-02-22
  • 1970-01-01
  • 2018-03-10
  • 2016-12-25
相关资源
最近更新 更多