api - 使用 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
解决方案
您的网址没有按照您尝试的方式正确构建。
这是执行此操作的一种方法的示例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
函数。
推荐阅读
- reactjs - 在 Amplify 数据存储中批量插入
- reactjs - 如何从 localStorage 获取对象?
- css - Wordpress elementor如何为小部件中的不同div分配渐变背景
- perl - perl - Hash::Merge 在散列中重复相同的列表,而不是将它们放置一次 - 继续:
- python - Apscheduler 在另一个脚本上添加作业错误
- python - 用同名文件夹覆盖导入的包
- java - Intellij - 自动导入的预定义类型
- bash - 检查bash中文件名路径中是否存在子文件夹
- javascript - ReactJS:在表单内的选择中显示列表
- java - 如何为不同的分辨率更改 Java swing GUI?