flutter - Http Post 在 Postman 中工作,但在 Flutter 中没有

标签 flutter dart http-post postman

编辑:我找到了解决方案并回答了这个问题。
我正在尝试 POST到我的应用程序中的 Instagram 的 API。 POST在 Postman 中工作正常,但在我的 flutter 代码中,我总是得到一个错误响应:{error_type: OAuthException, code: 400, error_message: Invalid platform app} .我一定是在 flutter 中做错了POST但我不知道是什么。
这是我的 postman POST (每次都有效)以及正确的 body react :
Postman Data
Postman Data 2
这是我的 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"}

最佳答案

试试这个

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

关于flutter - Http Post 在 Postman 中工作,但在 Flutter 中没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64668241/

相关文章:

flutter - 如何在 TextSpan 中 backrgroundColor full?

java - 如何从Java中的asynctask访问外部类中的公共(public)变量?

node.js - 无法让nodeJS Express post显示正文数据

flutter - 如何使用 flutter 发布字符串和列表的映射

flutter - 未处理的异常 : InternalLinkedHashMap<String, Object>' 不是 Map<String, String> 类型的子类型

dictionary - 将 Form 与 PageView 一起使用会在每个页面 Flutter 时覆盖以前的表单数据

java - 如何检查 List<BasicNameValuePair> 是否包含键?

flutter - 当焦点位于 TextField 且键盘在 Flutter 中打开时,如何将内容上推?

flutter - 在文本字段中输出光标的位置

flutter - 如何在 Flutter 中将应用程序的背景颜色设置为线性渐变?