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