在Expo Go中无法设置Google OAuth 2.0
我正在开发一个React应用。Google OAuth 2.0 登录在网页端可用。然而,在Android(Expo Go)上,我始终遇到这个错误:
访问被阻止:
授权错误。
错误400: invalid_request
请求详情:
redirect_uri=exp://192.168.50.36:8081
flowName=GeneralOAuthFlow
在Android客户端设置(Google Cloud Console)中:
- SHA-1是正确的,
- 包名是 "host.exp.exponent"(专用于Expo Go)。
在.env:所有客户端ID都已填写。
我已经尝试过使用useProxy,但没有效果。我也尝试过配置redirectURI,但同样没有效果。
下面是login.tsx文件中的一个关键代码片段(在这段代码之后,你将被重定向到欢迎页,所以这没关系。):
import { View } from "react-native";
import { Button, Card, Screen, Text } from "@/shared/ui";
import { spacing } from "@/shared/theme";
import { useEffect } from "react";
import * as Google from 'expo-auth-session/providers/google';
import * as WebBrowser from 'expo-web-browser';
import { storage } from '@/shared/storage';
import { useAppData } from "@/features/app-data/app-data-provider";
import { router } from "expo-router";
WebBrowser.maybeCompleteAuthSession();
export default function LoginScreen() {
const { setUser, session } = useAppData();
const [request, response, promptAsync] = Google.useAuthRequest({
webClientId: process.env.EXPO_PUBLIC_GOOGLE_WEB_CLIENT_ID,
androidClientId: process.env.EXPO_PUBLIC_GOOGLE_ANDROID_CLIENT_ID,
scopes: ['profile', 'email'],
});
(..other code...)
}
解决方案
useProxy不再起作用的问题在于它已经被弃用,现在你应该使用deepLinks。你需要在app.json中声明你的scheme名称,并在Google Cloud Console中将其加入白名单(类似com.yourname.appname)。
关于AuthSession,官方文档里还有更多说明,就在这里,还有一个指南。希望对你有帮助。
P.S.:如果你在Expo Go应用中测试你的应用,效果未必好,你可能需要切换到开发构建(development build)才行。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。