在尝试在我的 React Native 应用程序中实现 google 登录(不使用 expo)时,我遇到了一个问题,并且我正在努力了解如何使用 Google 云 OAuth 2.0 客户端正确设置整个过程。
我设置了两个 OAuth 客户端:一个 Android 和一个 Web 应用程序。我确保 Android 客户端的 SHA-1 指纹是正确的。
根据我的理解(来自各种教程和来源),我必须使用 @react-native-google-signin/google-signin 包在我的 React Native 项目中使用 Web 应用程序 ID,然后在检索一些令牌后发送到我的Strapi 后端并使用 Android 客户端 ID 来验证它并执行其余的登录逻辑。
用于选择帐户的 Android 弹出窗口正确显示,如下面的屏幕截图所示,但我收到以下错误:Google 登录错误 [com.google.android.gms.common.api.ApiException: DEVELOPER_ERROR]
我尝试过使用 Android 客户端密钥,但仍然遇到相同的错误。代码如下:
import { GoogleSignin } from '@react-native-google-signin/google-signin';
GoogleSignin.configure({
webClientId: '**REDACTED**.apps.googleusercontent.com',
offlineAccess: true,
});
const signInWithGoogle = async () => {
try {
await GoogleSignin.hasPlayServices(); //executes normally
const userInfo = await GoogleSignin.signIn(); //here is where the error happens
// @ts-ignore
const { idToken } = userInfo;
const response = await axios.post(`${STRAPI_URL}auth/google`, {
idToken,
});
console.log('User signed in', response.data);
ToastAndroid.show('Google login success', ToastAndroid.SHORT);
} catch (error) {
console.log(error);
console.error('Google Sign-In Error', error);
}
};
其中 REDACTED 是网络客户端 ID 或 Android ID(我都尝试过)。
我的 Android 清单:
我的 build.gradle 也将其包含在依赖项中:
implementation 'com.google.android.gms:play-services-auth:21.2.0'
我的 Strapi 逻辑与它无关,因为错误发生在
const userInfo = await GoogleSignin.signIn(); //here is where the error happens
,我很乐意根据要求包含它,但我认为没有必要。
我还将附上两个谷歌云控制台客户端的屏幕截图。请注意,我没有在
Authorized redirect URIs
列表中添加任何 URI,其中列出的 url 适用于使用相同客户端 ID 的其他应用程序。所以也许这就是我所缺少的。
我已经解决了这个问题。我必须确保 Web 应用程序 OAuth 2 客户端中的“授权重定向 URI”列表为空。