【发布时间】:2019-05-31 03:16:45
【问题描述】:
我正在尝试使用 http 在 Flutter 中发送发布请求。
我想使用 OAuth2 登录到自托管的 WordPress 安装。
在第一次请求时它会成功,我会登录并获取 cookie。但是在第二个请求中,我尝试在标头中设置响应的 cookie 并获取令牌,但响应为 302,我认为 cookie 或会话没有在标头中设置或类似的东西。
Swift 工作版本:
https://github.com/wlcdesigns/iOS-WP-OAuth/blob/master/iOS%20WP%20OAuth/OAuthWP.swift
这是请求:
import 'package:http/http.dart';
ajaxPost() async {
String apiURL = "http://example.com/app/oauth/authorize";
String username = 'admin';
String password = 'pass';
final identifier = "xxxxxxxxxxx";
final secret = "xxxxxxxxxxx";
var requestBody = {
'client_id': identifier,
"user_login": username,
"user_password": password,
'wpoauth_login': "1",
};
var headers = {
'Accept': 'application/json',
};
Response r = await post(
apiURL,
body: requestBody,
headers: headers
);
print(r.statusCode);
print(r.body);
var reqBody = {
'client_id': identifier,
'ios_wp_oauth': '1',
'response_type': 'code',
};
String rawCookie = r.headers['set-cookie'];
if (rawCookie != null) {
int index = rawCookie.indexOf(';');
headers['cookie'] =
(index == -1) ? rawCookie : rawCookie.substring(0, index);
}
Response res = await post(
apiURL,
body: reqBody,
headers: headers
);
print(res.statusCode);
print(res.body);
}
【问题讨论】:
标签: http post oauth-2.0 dart flutter