我正在尝试让我的 expo AuthSession 在我的 React Native 应用程序中工作(使用谷歌注册)。我一直在 YouTube 上关注此剪辑 ( https://www.youtube.com/watch?v=YX7IWOQIKA0 ),但当我尝试导航到登录屏幕时,就会出现错误。
我收到的错误消息是:
无法使用 AuthSession 代理,因为未定义项目全名。首选 AuthRequest(将 useProxy 选项设置为 false)与应用程序的 Expo Development Client 版本结合使用。要继续使用 AuthSession 代理,请使用 projectNameForProxy 选项指定项目全名 (@owner/slug)。
注释:
该应用程序总体运行正常,但当我尝试导航到存在以下代码的登录屏幕时,该应用程序完全崩溃。我已检查 Google Cloud 凭据中的“https://auth.expo.io/@owner/slug”是否正确。我是否错过了添加一些我应该添加的内容?我还应该补充一点,我的目标是让它在 IOS 和 Android 设备上都能工作。
与该问题相关的代码位于我的 LoginScreen.tsx 中,如下所示:
进口:
import * as WebBrowser from 'expo-web-browser';
import * as Google from 'expo-auth-session/providers/google';
WebBrowser.maybeCompleteAuthSession();
GoogleAuth请求:
const [request, response, promptAsync] = Google.useAuthRequest({
expoClientId: '*THE CLIENT ID FROM GOOGLE CLOUD CREDENTIALS IS PASTED HERE*',
iosClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
androidClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
webClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
});
按“继续使用 Google”时运行的代码:
const signUpGoogleHandler = async () => {
const response = await promptAsync();
if (response.type === 'success') {
const {access_token } = response.params;
console.log('res params ', access_token);
}
console.log('test123');
const provider = new GoogleAuthProvider();
await signInWithRedirect(auth, provider);
getRedirectResult(auth)
.then((result) => {
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
const user = result.user;
}).catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
const email = error.customData.email;
const credential = GoogleAuthProvider.credentialFromError(error);
})
}
如何在 Google Cloud 中设置我的 Web 应用程序的客户端 ID(授权重定向 URI 写作 https://auth.expo.io/@myusername/slugname ): Client ID for Web application
最佳答案
首先,将第二个参数添加到 Google.useAuthRequest
中,如下所示
const [request, response, promptAsync] = Google.useAuthRequest({
expoClientId: '*THE CLIENT ID FROM GOOGLE CLOUD CREDENTIALS IS PASTED HERE*',
iosClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
androidClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
webClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
},{
projectNameForProxy: "@owner/slug"
});
然后,在调用 promptAsync
方法时,向它们传递以下对象:
{
projectNameForProxy: "@owner/slug"
}
注意:将 @owner/slug
替换为正确的值。
关于react-native - 尝试实现 Expo AuthSession 时出现 AuthSession 代理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74501197/