react-native - 无法从 Web View 中恢复控制以在 OAuth 流程中对 native 应用程序使用react

标签 react-native oauth-2.0 react-native-android appauth

我正在尝试使用 FormidableLab 的 react-native-app-auth 实现 Google OAuth 2 登录我的 native android 应用程序中的库,如下所示:

  googleLoginPressed = async () => {
    const config = {
      serviceConfiguration: {
        authorizationEndpoint: 'https://accounts.google.com/o/oauth2/v2/auth', tokenEndpoint: 'https://accounts.google.com/o/oauth2/v2/auth', 
      },
      clientId: app_params.GOOGLE_CLIENT_ID, redirectUrl: 'https://<my_domain>/oauth/google',
      scopes: ['openid', 'profile', 'email'], additionalParameters: { 'response-type': 'code' },
    };
    try {
      const result = await authorize(config);
    } catch (error) {
      console.log(error);
    }
  }
这会使用 Google 的登录页面调用 Web View ,我可以成功验证自己的身份。然后 Google 正确重定向到我的 oauth 回调端点并填充 oauth code在重定向 url 中,就像它应该的那样。在这一点上,我期待 react-native-app-auth将控制权从 webview 返回到应用程序。相反,Web View 在重定向 url 页面上保持打开状态。
我在AndroidManifest.xml下添加了必要的网站关联配置和以下代码 MainActivity.java检查是否将控制权从重定向 url 返回给应用程序:
  @Override
  public void onNewIntent(Intent intent) { // this is not getting hit
    super.onNewIntent(intent);
    Uri appLinkData = intent.getData();

    if (appLinkData != null) {
      bar = appLinkData.getPath();
    }
  }
到目前为止我尝试过的
  • 我确保我的应用程序可以打开 Universal links .所以website association必须工作正常。
  • 还尝试在 iOS 上复制整个设置。结果一样。 webview 显示 Google 正确重定向到 oauth 端点,但应用程序无法重新获得控制权。

  • 如何将控制权从 oauth Web View 返回到我的 react-native 代码?

    最佳答案

    如果您使用声明的 HTTPS 方案,这是移动应用程序最推荐的安全选项,您可能还需要登录后的插页式页面,以触发通用链接。
    我的 blog post有更多的信息和一个你可以运行的代码示例,尽管它使用的是普通的 Kotlin 而不是 React Native。

    关于react-native - 无法从 Web View 中恢复控制以在 OAuth 流程中对 native 应用程序使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63122087/

    相关文章:

    react-native - 如何为 React Native 应用程序创建 Realm DB?

    android - 如何将 APK 发布到 Google 开发者控制台

    javascript - 从服务器获取数据时请求的值的属性名称不是对象

    oauth - 如何使用授权码从 Battle.net OAuth 请求访问 token ?

    react-native - node_modules/expo/AppEntry.js : Transformer. 变换不是函数

    react-native - 为什么 ref 在 FlatList 组件中不起作用?

    oauth-2.0 - OKTA - OAuthError : Illegal value for redirect_uri parameter

    python - 使用 python-oauth2 签署请求

    react-native - 从 react-native 链接中排除包

    ios - 想要将数据传递到其他组件-ListView