扑来:err_name_not_resolved通过自定义方案重定向时

发布于 2025-01-26 23:44:58 字数 2276 浏览 5 评论 0原文

我正在尝试通过Twitch登录,为此,我正在使用 https://pub.dev/packages/packages/url_launcher https://pub.dev/packages/uni_links 。该代码在iOS上运行良好,但在Android上不能很好地工作。

该过程是:

  • 单击“使用Twitch登录”
  • 通过lainingurl(_url)
  • 接受Twitch弹出式弹出式授权
  • 重定向到我的后端服务器后端的
  • 回调:创建/检索用户
  • 后端:返回:带有URL的307 http:customscheme:// auth/twitch?access_token = ...
  • 倾听深链接:关闭浏览器并将令牌存储在

Android上的设备中,当我的浏览器尝试重定向时使用我的CustomScheme,设备浏览器会返回错误:

Webpage not available

The webpage at
http://customscheme//auth/twitch?access_token=XXX
could not be loaded because:

net::ERR_NAME_NOT_RESOLVED

customscheme被我替换为我的,当我使用命令测试时,它可以工作:

adb shell am启动-a android.intent.intent.intart.action 。

因此,看来它不了解WebView的自定义方案,而是像简单的HTTP URL一样处理它。

这是我的代码:

  void logWithTwitch() async {
    assert(dotenv.env['TWITCH_CLIENT_ID'] != null);
    assert(dotenv.env['TWITCH_REDIRECT_URI'] != null);

    final String twitchClientId = dotenv.env['TWITCH_CLIENT_ID']!;
    final String twitchRedirectUri = dotenv.env['TWITCH_REDIRECT_URI']!;
    final List<String> scopes = [
      'user:read:email',
    ];
    final Uri _url = Uri(
        scheme: 'https',
        host: 'id.twitch.tv',
        path: 'oauth2/authorize',
        queryParameters: {
          'client_id': twitchClientId,
          'redirect_uri': twitchRedirectUri,
          'response_type': 'code',
          'scope': scopes.join(' '),
          'force_verify': 'true',
        });

    if (await canLaunchUrl(_url)) {
      await launchUrl(_url);
    } else {
      print('cannot open url');
    }
  }

...

_sub = linkStream.listen((String? link) {
    if (link == null) {
        return;
    }

    final _url = Uri.parse(link);
    if (_url.host == 'auth' && _url.path == '/twitch') {
        closeInAppWebView();
        // Handle Authentication
    }
}, onError: (err) {
    // Handle exception by warning the user their action did not succeed
});

谢谢您的帮助!

I am trying to login via Twitch and, to do that I am using https://pub.dev/packages/url_launcher and https://pub.dev/packages/uni_links. The code works well on iOS, but not on Android.

The process is:

  • Click on "Sign in with Twitch"
  • Opens a browser via launchUrl(_url)
  • Accept authorization of Twitch popup
  • Redirects to the callback of my backend server
  • Backend: Create/Retrieve the user
  • Backend: Returns a 307 HTTP with url: customscheme://auth/twitch?access_token=...
  • Listens the Deep Link: Close the browser and stores the token in the device

On Android, when my browser tries to redirect with my customscheme, the device browser returns an error:

Webpage not available

The webpage at
http://customscheme//auth/twitch?access_token=XXX
could not be loaded because:

net::ERR_NAME_NOT_RESOLVED

The customscheme is replaced by mine, and it works when I test it with the command:

adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "customscheme://auth/twitch".

So it seems it does not understand the custom scheme from the webview, and handle it like a simple HTTP url.

Here is my code:

  void logWithTwitch() async {
    assert(dotenv.env['TWITCH_CLIENT_ID'] != null);
    assert(dotenv.env['TWITCH_REDIRECT_URI'] != null);

    final String twitchClientId = dotenv.env['TWITCH_CLIENT_ID']!;
    final String twitchRedirectUri = dotenv.env['TWITCH_REDIRECT_URI']!;
    final List<String> scopes = [
      'user:read:email',
    ];
    final Uri _url = Uri(
        scheme: 'https',
        host: 'id.twitch.tv',
        path: 'oauth2/authorize',
        queryParameters: {
          'client_id': twitchClientId,
          'redirect_uri': twitchRedirectUri,
          'response_type': 'code',
          'scope': scopes.join(' '),
          'force_verify': 'true',
        });

    if (await canLaunchUrl(_url)) {
      await launchUrl(_url);
    } else {
      print('cannot open url');
    }
  }

...

_sub = linkStream.listen((String? link) {
    if (link == null) {
        return;
    }

    final _url = Uri.parse(link);
    if (_url.host == 'auth' && _url.path == '/twitch') {
        closeInAppWebView();
        // Handle Authentication
    }
}, onError: (err) {
    // Handle exception by warning the user their action did not succeed
});

Thank you for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是伱的 2025-02-02 23:44:58

将项目更新为最新版本后,我遇到了同样的问题。
似乎问题来自直接在Flutter应用程序中启动WebView。
找到了解决方法:
在launchurlstring函数中使用启动模式选项:
lainingurlstring(_url,模式:launchmode.externalapplication)

I got the same problem after updating my project to the newest version.
Seems like the problem comes from launching the webview directly in the flutter app.
found a workaround for it:
use the launchmode option in the launchUrlString Function:
launchUrlString(_url,mode: launchMode.externalApplication)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文