首页 > 解决方案 > Flutter:登录 Instagram 时未获取代码(Instagram 登录不起作用)

问题描述

我正在使用颤振,我想在我的应用程序中使用 Instagram 登录。所以我正在按照本教程进行 Instagram 登录,

我登录成功但没有获取用户详细信息,它正在打开重定向 URI ( https://www.google.com/ ) 并且没有获取代码和访问令牌。

如果您知道任何事情,请帮助我。

注意:我正在使用flutter_webview_plugin在flutter 中加载webview。

这是为Instagram 登录打开 webview 的代码->

我的日志print("代码 ========== 代码"); 不再打印

  Future<Token> getToken(String appId, String appSecret) async {

  Stream<String> onCode = await _server();
  print("_server ========== _server");
  String url =
      "https://api.instagram.com/oauth/authorize?client_id=$appId&redirect_uri=https://www.google.com/&response_type=code";
  final flutterWebviewPlugin = new FlutterWebviewPlugin();
  flutterWebviewPlugin.launch(url);
  final String code = await onCode.first;
  print("Code ========== Code");
  print("Code -> "+code);
  final http.Response response = await http.post(
      "https://api.instagram.com/oauth/access_token",
      body: {"client_id": appId, "redirect_uri": "https://www.google.com/", "client_secret": appSecret,
      "code": code, "grant_type": "authorization_code"});

  flutterWebviewPlugin.close();


  return new Token.fromMap(json.decode(response.body));
}

Future<Stream<String>> _server() async {
  final StreamController<String> onCode = new StreamController();
  HttpServer server =
  await HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 8585);
  server.listen((HttpRequest request) async {
    final String code = request.uri.queryParameters["code"];
    request.response
      ..statusCode = 200
      ..headers.set("Content-Type", ContentType.HTML.mimeType)
      ..write("<html><h1>You can now close this window</h1></html>");
    await request.response.close();
    await server.close(force: true);
    onCode.add(code);
    await onCode.close();
  });
  return onCode.stream;
}

标签: flutterinstagraminstagram-apiflutterwebviewplugin

解决方案


iOS 配置如果有人看,请将下面的代码添加到 Info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost:8585</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>

推荐阅读