【问题标题】:Http Post is working in Postman, but not FlutterHttp Post 在 Postman 中工作,但在 Flutter 中没有
【发布时间】:2021-02-16 10:47:17
【问题描述】:

编辑:我找到了解决方案并回答了问题。

我正在我的应用程序中尝试 POST 到 Instagram 的 API。 POST 在 Postman 中运行良好,但在我的颤动代码中,我总是收到错误响应:{error_type: OAuthException, code: 400, error_message: Invalid platform app}。我的颤振POST一定是做错了什么,但我不知道是什么。

这是我的邮递员POST(每次都有效)以及正确的身体反应:

这是我的 Dart/Flutter 代码,它给了我错误:

        var clientID = '708XXXXXXXX';
        var clientSecret = '37520XXXXXXXXXXXXX';
        var grantType = 'authorization_code';
        var rediUrl = 'https://httpstat.us/200';
        
        var urlTwo = 'https://api.instagram.com/oauth/access_token';
        var body = json.encode({
          'client_id': clientID,
          'client_secret': clientSecret,
          'grant_type': grantType,
          'redirect_uri': rediUrl,
          'code': _accessCode
        });
                
        print('Body: $body');

        var response = await http.post(urlTwo,
          headers: {
            'Accept': '*/*',
            'Content-Type': 'application/json',
          },
          body: body,
        );

        print(json.decode(response.body));

我在另一个线程上读到了这个错误,他们说要发布为 x-www-formurlencoded.. 当我将 Content-Type 切换到这个时,我得到了同样的错误。

我在 Flutter 中遇到的错误:

{error_type: OAuthException, code: 400, error_message: Invalid platform app}

我进行了三次检查,并且在 Postman 和 Flutter 中使用了所有相同的值。我已经尝试将值作为字符串和数字。

当我打印正文时:

Body: {"client_id":"70000edited","client_secret":"37520634edited","grant_type":"authorization_code","redirect_uri":"https://httpstat.us/200","code":"AQCs-H3cIU0aF1o2KkltLuTmVMmJ-WJnZIhb9ryMqYedited"}

【问题讨论】:

  • 您是否尝试像这样更改redirect_uri:localhost:5001
  • @Akif 不,redirect-uri 必须与我在 Facebook 开发人员仪表板中设置的 URL 匹配,我设置的那个在 Postman 中运行良好。我使用的那个会自动发送 200 响应进行测试。

标签: flutter dart http-post postman


【解决方案1】:

尝试一次

final response = await http.post(urlTwo,
   body: body,
   headers: {
     "Accept": "application/json",
     "Content-Type": "application/x-www-form-urlencoded"
   },
   encoding: Encoding.getByName("utf-8"));

【讨论】:

  • 我完全试过了,结果还是一样。
【解决方案2】:

我终于用Map 让它工作了。这很奇怪,因为其他尝试我也使用了地图(我一直在尝试无数堆栈溢出的答案)。无论如何,这是我使用的工作代码:

        var urlTwo = 'https://api.instagram.com/oauth/access_token';
        var clientID = '7080000';
        var clientSecret = '375XXXXXX';
        var grantType = 'authorization_code';
        var rediUrl = 'https://httpstat.us/200';

        var map = new Map<String, dynamic>();
        map['client_id'] = clientID;
        map['client_secret'] = clientSecret;
        map['grant_type'] = grantType;
        map['redirect_uri'] = rediUrl;
        map['code'] = _accessCode;

        http.Response response = await http.post(
          urlTwo,
          body: map,
        );

        var respData = json.decode(response.body);
        print(respData);

【讨论】:

    猜你喜欢
    • 2020-04-28
    • 1970-01-01
    • 2015-11-08
    • 2020-01-26
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多