【发布时间】:2021-11-21 08:41:59
【问题描述】:
我正在使用以下代码在我的 dart 应用中为 Wikimedia 实施 OAuth 2 授权流程:
String jsonString = jsonEncode(<String, String>{
'grant_type' : 'authorization_code',
'redirect_uri' : Uri.encodeFull(redirectUri),
'code' : authCode,
'client_id' : CLIENT_ID,
'client_secret' : clientSecret,
});
String paramName = 'param';
String formBody = paramName + '=' + Uri.encodeQueryComponent(jsonString);
List<int> bodyBytes = utf8.encode(formBody);
Future<http.Response> response = http.post(
Uri.parse('https://meta.wikimedia.org/w/rest.php/oauth2/access_token'),
headers: <String, String>{
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"Content-Length" : bodyBytes.length.toString()
},
body: bodyBytes,
);
对此的回应是:
{ "error": "invalid_request", "error_description": "请求是 缺少必需的参数,包括无效的参数值, 多次包含一个参数,或者格式错误。",
"hint": "检查 `grant_type` 参数", "message": "请求 缺少必需的参数,包含无效的参数值, 包含不止一次的参数,或者格式错误。” }
这可能与内容类型仍然是 JSON 的事实有关,即使我在标头中将其定义为 application/x-www-form-urlencoded 或因为内容长度为 -1。
【问题讨论】:
标签: flutter http dart oauth-2.0 mediawiki-api