Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do wordpress api authentication in flutter

I am making a flutter application in which in need to fetch data from backened API which is made in wordpress.Now in postman i only need to insert client key and client secret in Oauth 1 authentication and it works fine.But in flutter application it tell that the signature data is invalid.Why ?

I followed official guide from woocommerce Api but i failed.How can i make wordpress api in flutter in dart?I am new to flutter and this is very important for me.So how can i fetch data ?Is there any method to achieve what i want ?

like image 818
Abhi Singh Avatar asked Feb 04 '26 08:02

Abhi Singh


1 Answers

As per my understanding, you are looking for something like this

  1. You want to display the products from wooCommerce using REST API.
  2. And you want that to be done in Flutter Dart.
  3. Auth for users.

The very first thing will do is Auth the user using Username and Password so to do that we have to do something like this

For Auth you should install the JWT plugin name JWT Authentication for WP-API in WordPress

Then use this URL in the Flutter

    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;
  }

This function fetches the data from the wooCommerce REST API endpoints and stores in the 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();
});

}

Now you should call this future getCategoryData method like this

void initState() {
setState(() {
  this.getCategoryData();
});
super.initState();

}

I have created a class for 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']);
  }


}

Thanks, I hope this will help you

like image 147
HaSnen Tai Avatar answered Feb 07 '26 02:02

HaSnen Tai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!