首页 > 解决方案 > 如何在flutter中进行wordpress api认证

问题描述

我正在制作一个颤振应用程序,其中需要从 wordpress 中制作的支持 API 获取数据。现在在邮递员中,我只需要在 Oauth 1 身份验证中插入客户端密钥和客户端密码,它工作正常。但在颤振应用程序中它告诉签名数据无效。为什么?

我遵循了 woocommerce Api 的官方指南,但我失败了。我怎样才能让 wordpress api 在飞镖中颤振?我是颤振新手,这对我来说非常重要。那么我怎样才能获取数据?有什么方法可以实现我的目标吗?想 ?

标签: flutterwoocommerce-rest-api

解决方案


据我了解,您正在寻找这样的东西

  1. 您想使用 REST API 显示来自 wooCommerce 的产品。
  2. 你希望在 Flutter Dart 中完成。
  3. 对用户进行身份验证。

首先要做的是使用用户名和密码来验证用户,所以我们必须做这样的事情

对于 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']);
  }


}

谢谢,我希望这对你有帮助


推荐阅读