Flutter WooCommerce客户注册使用Dio失败



我正在为客户构建flutter-woocommerce商店应用程序。我想使用Dio()创建/注册一个新客户,我正在使用WordPress插件JWT Authentication for WP REST API进行身份验证。如果我想访问客户的URL,这里会显示以下响应。URL=";CCD_ 3";

{
code: "woocommerce_rest_cannot_view",
message: "Sorry, you cannot list resources.",
data: {
status: 401
}
}

我的客户模型文件是:

class CustomerModel {
String email;
String firstName;
String lastName;
String password;
CustomerModel(
{required this.email,
required this.firstName,
required this.lastName,
required this.password});
Map<String, dynamic> toJson() {
Map<String, dynamic> map = {};
map.addAll({
'email': email,
'first_name': firstName,
'last_name': lastName,
'password': password,
'username': email
});
return map;
}
}

我的API服务模式创建客户是:

Future<bool> createCustomer(CustomerModel model) async {
var authToken =
base64.encode(utf8.encode(Config.key + ':' + Config.sceret));
print(authToken);
bool ret = false;
try {
print("${Config.url+ Config.customerURL}");
var response = await Dio().post(Config.url + Config.customerURL,
data: model.toJson(),
options: new Options(headers: {
HttpHeaders.authorizationHeader: 'Basic $authToken',
HttpHeaders.contentTypeHeader: 'application/json'
}));
print(response.statusCode);
if (response.statusCode == 201) {
ret = true;
}
} on DioError catch (e) {
if (e.response?.statusCode == 404) {
ret = false;
} else {
ret = false;
}
}
return ret;
}

我的型号Perameter数据采用以下格式:

{
email: qas@gmail.com,
first_name: qasim,
last_name: ali,
password: qasim123,
username: qas@gmail.com
}

我的createCustomer方法总是返回false:::我的意思是Dio().Post()没有成功工作,请指导

代替Basic

HttpHeaders.authorizationHeader: 'Basic $authToken',

在标题中使用Bearer

HttpHeaders.authorizationHeader: 'Bearer $authToken',

我有同样的问题,并设法通过删除授权标头并将密钥和机密附加到post路径来解决它,我只是不确定这是否是实现它的安全方法。请参阅下面的代码

var response = await Dio().post(Config.url + Config.customerURL + '?consumer_key=' + Config.key + '&consumer_secret=' + Config.secret,
data:model.toJson(),
options: Options(headers: {
//HttpHeaders.authorizationHeader:'Bearer: $authToken',
HttpHeaders.contentTypeHeader: 'application/json',
},

我添加回authorazation标题,它也在工作。

相关内容

最新更新