flutter - 如何在flutter中进行wordpress api认证
问题描述
我正在制作一个颤振应用程序,其中需要从 wordpress 中制作的支持 API 获取数据。现在在邮递员中,我只需要在 Oauth 1 身份验证中插入客户端密钥和客户端密码,它工作正常。但在颤振应用程序中它告诉签名数据无效。为什么?
我遵循了 woocommerce Api 的官方指南,但我失败了。我怎样才能让 wordpress api 在飞镖中颤振?我是颤振新手,这对我来说非常重要。那么我怎样才能获取数据?有什么方法可以实现我的目标吗?想 ?
解决方案
据我了解,您正在寻找这样的东西
- 您想使用 REST API 显示来自 wooCommerce 的产品。
- 你希望在 Flutter Dart 中完成。
- 对用户进行身份验证。
首先要做的是使用用户名和密码来验证用户,所以我们必须做这样的事情
对于 Auth,您应该在 WordPress 中安装 JWT 插件名称 JWT Authentication for WP-API
然后在 Flutter 中使用这个 URL
Future<http.Response> login(String username, String password) async {
final http.Response response = await http.post('https://domina-name/wp-json/jwt-auth/v1/token?username=abc&password=xyz');
print(response);
return response;
}
此函数从 wooCommerce REST API 端点获取数据并存储在列表中
List<CatService> category;
Future<void> getCategoryData() async {
var res = await http.get(
"https://domain-name/wp-json/wc/v3/products/categories?per_page=100&consumer_key=xxxxxxxxxxxxxxxxxxxxx&consumer_secret=xxxxxxxxxxxxxxx&page=1");
setState(() {
var data = json.decode(res.body);
var list = data as List;
print("List of cat $list");
categoryList =
list.map<CatService>((json) => CatService.fromJson(json)).toList();
category = categoryList
.where((data) => data.count > 0 && data.catName != 'Uncategorized')
.toList();
});
}
现在你应该像这样调用这个未来的 getCategoryData 方法
void initState() {
setState(() {
this.getCategoryData();
});
super.initState();
}
我为 CatService 创建了一个类
class CatService {
int catId;
String catName;
int count;
CatService({this.catId, this.catName,this.count});
factory CatService.fromJson(Map<String, dynamic> json) {
return CatService(catId: json['id'], catName: json['name'],count: json['count']);
}
}
谢谢,我希望这对你有帮助
推荐阅读
- java - 如何以编程方式查找分片 MongoDB 集群中的分片数量
- angular - 在 ubuntu 16.4.5 中以 Angular 5 安装角材料(错误:“ElementRef”类型不是通用的。)
- python - Travis CI Python 在 osx 上构建失败 - “未安装 2.7”
- ssl - How to use private key and public key with chain in a mutual ssl authentication
- android - How should I set the scaleType of an ImageView, when I use Glide
- javascript - Javascript:通过遍历数组来检查复选框是否被选中
- angular5 - 当子路由更改时,Angular 5 Parent 会重新加载
- javascript - javascript banner - close button doesn't work corectly
- c# - 如何使用 AutoMapper 进行嵌套映射?
- postgresql - 在 postgresql 的子查询中使用执行