首页 > 解决方案 > 使用 getx flutter 远程访问 woocommerce

问题描述

我正在尝试通过 api 连接到我的 woocommerce,但它显示错误 401

class RemoteServices{
static var client = http.Client();
static Future<List<Welcome>> fetchProducts() async{
var response =await client.get("https://mywebsite/wp-json/wc/v3/products",headers: <String,String>{'Usermname':('ck_key'),'Password':'cs_key' });
  if(response.statusCode==200){
    var jsontString = response.body;
    return welcomeFromJson(jsontString);
    
  }
  else{
    print("error api");
    print(response.statusCode);
  }
}
}

它总是向我显示错误 401

标签: apiflutterdart

解决方案


您的网址没有按照您尝试的方式正确构建。

这是执行此操作的一种方法的示例GetConnect

final map = {'apikey': 'yourApiKey'}; //equivalent to adding apiKey=yourApiKey to the url
final response = await client.get("https://mywebsite/wp-json/wc/v3/products", headers: map);

您也不必在get调用中使用 headers 属性。如果您不熟悉调用 APIs,那么更简单的开始方法是将完整的完整 url 传递到get方法中,并且效果也很好。

无论哪种方式,您都需要确认浏览器中正确的 url 应该是什么,然后确保将其传递给get函数。


推荐阅读