React-native google auth android DEVELOPER_ERROR 代码10
React-native google auth android DEVELOPER_ERROR Code 10
我做了什么:
react-native init testAuthGoogle && cd testAuthGoogle && (cd android; ./gradlew signingReport)
创建一个新项目并显示 SHA1
> Task :app:signingReport
Variant: debugAndroidTest
Config: debug
Store: /Users/anand/ws/rn01/testAuthGoogle/android/app/debug.keystore
Alias: androiddebugkey
MD5: 20:F4:61:48:B7:2D:8E:5E:5C:A2:3D:37:A4:F4:14:90
SHA1: 55:88:11:06:2E:A3:CC:2C:4A:0D:EE:78:76:88:A6:F3:8C:AB:FF:88
SHA-256: FA:C6:17:45:DC:09:03:78:6F:B9:ED:E6:2A:96:2B:39:9F:73:48:F0:BB:6F:89:9B:83:32:66:75:91:03:3B:9C
Valid until: Tuesday, April 30, 2052
将 SHA1 复制到 firebase 并创建了 android 应用
已将 google-services.json
文件下载到 android/app/google-services.json
在android/build.gradle
中:
- 添加依赖项:
classpath("com.google.gms:google-services:4.3.3")
在android/app/build.gradle
中:
- 应用插件:
apply plugin: "com.google.gms.google-services"
- 添加了依赖项
implementation 'com.google.firebase:firebase-analytics:17.5.0'
和 implementation 'com.google.firebase:firebase-auth:19.4.0'
安装了其他依赖项:
npm install --save @react-native-firebase/app @react-native-firebase/auth @react-native-community/google-signin
从 google-services.json
文件复制了 client_id
以在 app.js 中使用它。
app.js
中的代码更改:
import React, { useEffect, useState } from 'react';
import { SafeAreaView, StyleSheet, ScrollView, View, Text, StatusBar } from 'react-native';
import { Colors } from 'react-native/Libraries/NewAppScreen';
import { GoogleSignin, GoogleSigninButton, statusCodes } from '@react-native-community/google-signin';
import auth from '@react-native-firebase/auth';
GoogleSignin.configure({ webClientId: '532405863926-94v4mgqg18ajc2g7tk6ttghvsnilooee.apps.googleusercontent.com' });
const App: () => React$Node = () => {
const [user, setUser] = useState();
const [authErr, setAuthErr] = useState('');
useEffect(() => auth().onAuthStateChanged((user) => setUser(user)), []);
onGoogleSignOut = async () => await auth().signOut()
getAuthErrorSnip = () => authErr ? <Text>{JSON.stringify(authErr)}</Text> : null
onGoogleSignIn = async () => {
try {
const user = await GoogleSignin.signIn();
await auth().signInWithCredential(auth.GoogleAuthProvider.credential(user.idToken));
} catch (error) {
setAuthErr(error);
}
}
return (
<SafeAreaView>
<ScrollView contentInsetAdjustmentBehavior="automatic" style={styles.scrollView}>
<View style={styles.body}>
<View style={styles.sectionContainer}>
<GoogleSigninButton onPress={onGoogleSignIn} ></GoogleSigninButton>
{getAuthErrorSnip()}
</View>
</View>
</ScrollView>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
body: {
backgroundColor: Colors.white,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
}
});
export default App;
当我 react-native run-android
并点击 Sign in
时我看到了什么:
这很可能是与您的 client_id 或 SHA-1 配置相关的错误,请查看 github 上与同一错误相关的 issue。
通过查看您分享的屏幕截图和代码,在我看来您使用了错误的 client_id,对于 google 网络客户端,我相信你应该使用
client_id
与 client_type : 3
而不是
client_id
与 client_type : 1
所以尝试在 app.js
文件中修复你的 client_id
,它应该可以工作。
我做了什么:
react-native init testAuthGoogle && cd testAuthGoogle && (cd android; ./gradlew signingReport)
创建一个新项目并显示 SHA1
> Task :app:signingReport
Variant: debugAndroidTest
Config: debug
Store: /Users/anand/ws/rn01/testAuthGoogle/android/app/debug.keystore
Alias: androiddebugkey
MD5: 20:F4:61:48:B7:2D:8E:5E:5C:A2:3D:37:A4:F4:14:90
SHA1: 55:88:11:06:2E:A3:CC:2C:4A:0D:EE:78:76:88:A6:F3:8C:AB:FF:88
SHA-256: FA:C6:17:45:DC:09:03:78:6F:B9:ED:E6:2A:96:2B:39:9F:73:48:F0:BB:6F:89:9B:83:32:66:75:91:03:3B:9C
Valid until: Tuesday, April 30, 2052
将 SHA1 复制到 firebase 并创建了 android 应用
已将 google-services.json
文件下载到 android/app/google-services.json
在android/build.gradle
中:
- 添加依赖项:
classpath("com.google.gms:google-services:4.3.3")
在android/app/build.gradle
中:
- 应用插件:
apply plugin: "com.google.gms.google-services"
- 添加了依赖项
implementation 'com.google.firebase:firebase-analytics:17.5.0'
和implementation 'com.google.firebase:firebase-auth:19.4.0'
安装了其他依赖项:
npm install --save @react-native-firebase/app @react-native-firebase/auth @react-native-community/google-signin
从 google-services.json
文件复制了 client_id
以在 app.js 中使用它。
app.js
中的代码更改:
import React, { useEffect, useState } from 'react';
import { SafeAreaView, StyleSheet, ScrollView, View, Text, StatusBar } from 'react-native';
import { Colors } from 'react-native/Libraries/NewAppScreen';
import { GoogleSignin, GoogleSigninButton, statusCodes } from '@react-native-community/google-signin';
import auth from '@react-native-firebase/auth';
GoogleSignin.configure({ webClientId: '532405863926-94v4mgqg18ajc2g7tk6ttghvsnilooee.apps.googleusercontent.com' });
const App: () => React$Node = () => {
const [user, setUser] = useState();
const [authErr, setAuthErr] = useState('');
useEffect(() => auth().onAuthStateChanged((user) => setUser(user)), []);
onGoogleSignOut = async () => await auth().signOut()
getAuthErrorSnip = () => authErr ? <Text>{JSON.stringify(authErr)}</Text> : null
onGoogleSignIn = async () => {
try {
const user = await GoogleSignin.signIn();
await auth().signInWithCredential(auth.GoogleAuthProvider.credential(user.idToken));
} catch (error) {
setAuthErr(error);
}
}
return (
<SafeAreaView>
<ScrollView contentInsetAdjustmentBehavior="automatic" style={styles.scrollView}>
<View style={styles.body}>
<View style={styles.sectionContainer}>
<GoogleSigninButton onPress={onGoogleSignIn} ></GoogleSigninButton>
{getAuthErrorSnip()}
</View>
</View>
</ScrollView>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
body: {
backgroundColor: Colors.white,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
}
});
export default App;
当我 react-native run-android
并点击 Sign in
时我看到了什么:
这很可能是与您的 client_id 或 SHA-1 配置相关的错误,请查看 github 上与同一错误相关的 issue。
通过查看您分享的屏幕截图和代码,在我看来您使用了错误的 client_id,对于 google 网络客户端,我相信你应该使用
client_id
与 client_type : 3
而不是
client_id
与 client_type : 1
所以尝试在 app.js
文件中修复你的 client_id
,它应该可以工作。