首页 > 解决方案 > 尝试在 React Native 中使用 GoogleSignIn,然后可能出现未处理的 Promise Rejection(id:错误:DEVELOPER_ERROR

问题描述

我已经使用此链接https://rnfirebase.io/auth/social-auth在 Firebase 上启用了 Google登录 我的应用程序编译得很好,但是当我按下登录按钮并选择一个 Google 帐户进行登录时,我得到了可能未处理的承诺拒绝(id:0):错误:DEVELOPER_ERROR https://rnfirebase.io/auth/social-auth 我有 Windows 10 和

react-native-cli: 2.0.1
react-native: 0.63.4
Node : v12.16.0

这是 App.js

    import React , {useState , useEffect} from 'react';
    import { Text TextInput, View } from 'react-native';
    import {ButtonTemplate} from '../assets/ButtonTemplate'
    import auth from '@react-native-firebase/auth';
    import { GoogleSignin } from '@react-native-community/google-signin';

    const App = () => {

    useEffect(()=> {
        GoogleSignin.configure({
            webClientId: 'xxxxxxxxxxxxxx.apps.googleusercontent.com',
        });
        })

    async function onGoogleButtonPress() {
            // Get the users ID token
            const { idToken } = await GoogleSignin.signIn();
        
            // Create a Google credential with the token
            const googleCredential = auth.GoogleAuthProvider.credential(idToken);
        
            // Sign-in the user with the credential
            return auth().signInWithCredential(googleCredential);
        }
    const googleLogin=()=>{
            console.log('Google SignIn');
            onGoogleButtonPress().then(() => console.log('Signed in with Google!'))
            }

    return (
    <ButtonTemplate 
                text={"Continue With Google"} 
                backgroundColor={'#5384ed'}
                fontSize={16}
                marginTop= {'3%'}
                iconText={'G'}
                justifyContent={'flex-start'}
                onPress={googleLogin}
                />
    )
    };

    export default App;

这是错误

    Possible Unhandled Promise Rejection (id: 0):
Error: DEVELOPER_ERROR
promiseMethodWrapper@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2258:45
signIn$@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:189098:72
tryCatch@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25048:23
invoke@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25221:32
tryCatch@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25048:23
invoke@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25121:30
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25131:21
tryCallOne@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27134:16
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27235:27
_callTimer@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:30674:17
_callImmediatesPass@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:30713:17
callImmediates@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:30930:33
__callImmediates@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2760:35
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2546:34
__guard@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2743:15
flushedQueue@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2545:21
flushedQueue@[native code]
callFunctionReturnFlushedQueue@[native code]

标签: javascriptreactjsfirebasereact-nativegoogle-signin

解决方案


我有两个问题第一个是上面,第二个是An OAuth2 client already exists for this package name and SHA-1 in another project.第二个问题是在 Firebase 中,同时在项目中添加 SHA1。我解决了这个问题

  1. 删除 debug.keystore
  2. 然后通过这个重新生成它keytool -genkey -v -keystore debug.keystore -alias androiddebugkey -storepass android -keypass android -keyalg RSA -validity 10000
  3. 然后keytool -keystore debug.keystore -list -v在给出位于android/App/build.gradle then signingConfigs there is store password
  4. 在 Firebase 我的项目中添加了 SHA1 和 SHA256
  5. 下载最新的 JSON 文件在我的项目android/App文件夹中添加最新的 JSON
  6. 然后清理gradlecd android && ./gradlew clean
  7. 卸载应用程序然后执行npx react-native run-android
  8. 现在它的工作:)

我使用了这个链接How to Implement Google Login in React Native with Firebase 也是一个 Youtube 视频Youtube 视频相关的上一个链接


推荐阅读