react-native - 尝试实现 Expo AuthSession 时出现 AuthSession 代理错误

标签 react-native google-cloud-platform oauth-2.0 expo expo-go

我正在尝试让我的 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/

相关文章:

c# - 向 Bearer 授权添加额外的逻辑

oauth-2.0 - 发送带有对象标签的 http 请求 header

node.js - 如何使用 Node.js 在远程服务器上对 google Sheets api 的用户进行身份验证?

react-native - 无法从 node_modules\uuidv4\build\lib\uuidv4.js : util could not be found within the project 解析模块 util

javascript - 数组 setState 和获取数组 React Native

google-app-engine - 如何使用GAE/SE go112将大文件上传到Google云端存储

docker - 谷歌云构建中两个容器之间的通信

react-native - 如何从使用 Expo 和 React Native 构建的应用程序连接到 SSID?

javascript - 屏幕导航标题不会显示在堆叠的抽屉导航中....React Navigation V2

node.js - 使用 node.js 从谷歌云存储中删除多个对象(文件和文件夹)的最佳做法是什么