如何在flutter中进行wordpress api身份验证

问题描述 投票:1回答:1

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

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

flutter woocommerce-rest-api
1个回答
0
投票

根据我的理解,你正在寻找这样的东西

  1. 您想使用REST API显示来自wooCommerce的产品。
  2. 你想要在Flutter Dart中完成。
  3. 为用户验证。

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

对于Auth,您应该在WordPress中为WP-API安装JWT插件名称JWT Authentication

然后在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中

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


}

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

© www.soinside.com 2019 - 2024. All rights reserved.