import { GoogleSignin, statusCodes } from '@react-native-google-signin/google-signin';
import auth from '@react-native-firebase/auth';
const signInWithGoogle = async () => {
try {
GoogleSignin.configure({
// This is example of webClientId
webClientId: 'example.apps.googleusercontent.com',
prompt: 'select_account',
});
// Check if your device supports Google Play
await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
// 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);
} catch (error) {
if (error.code === statusCodes.SIGN_IN_CANCELLED) {
alert("Access cancelled");
} else if (error.code === statusCodes.IN_PROGRESS) {
alert("The access procedure is already underway");
} else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
alert("Google Play Services not available");
} else {
alert(error.message);
}
}
}
如果需要,您可以在这里找到这两个库的文档:
https://www.npmjs.com/package/@react-native-google-signin/google-signin#public-api
https://rnfirebase.io/auth/social-auth#google
我的问题如下:
当我使用模拟器或手机在计算机中进行测试时,身份验证工作正常,而我在 Google Play 商店中发布的应用程序版本(显然具有相同的代码)无法让我登录。该代码会生成一个异常,被捕获在我的函数的“else”中,并且带有 error.message 的警报的结果是“DEVELOPER_ERROR”。为什么我会遇到这个问题?
(如果需要更多信息,请询问)。
我是如何解决的:
我通过将“应用程序签名密钥证书”和“上传密钥证书”的 SHA-1 添加到 firebase 项目来解决。
在哪里可以找到“应用签名密钥证书”和“上传密钥证书”的 SHA-1?
转到 Google Play 控制台并输入您的应用程序,在左侧部分中查找“设置”,然后单击“应用程序完整性”。 到达那里后,将出现“Google Play App Signature”项,点击右侧的“设置”。在这里您可以找到“应用程序签名密钥证书”和“上传密钥证书”的 SHA-1 代码
现在您需要在 Firebase 项目中添加两个 SHA-1 并更新 google.services.json 文件。
单击左侧菜单中的齿轮进入您的 Firebase 项目,然后单击“项目设置”,向下滚动到“您的应用程序”并添加您从 Google Play Console 获取的两个 SHA-1。添加两个 SHA-1 后,重新下载“google.services.json”并将其替换为您项目中的 SHA-1。
一旦完成,你就完成了。