我在我的React原生电子商务应用中使用了Woocommerce REST API库@woocommerce/woocommerce-rest-api。我还尝试过库https://github.com/JamesUgbanu/react-native-woocommerce-api。我正在localhost:8080上测试这一点。
在AndroidManifest.xml中,android:usesCleartextTraffic属性设置为"true"。
上述两个库都会生成这样的URL
http://localhost:8080/wp-json/wc/v3/products?oauth_consumer_key=ck_d1e9172ffce8f4016a3ae9b1ee8bce8b7a4c6365&oauth_nonce=ajJA9q13HYyZJU2TRMIqaeYU3vq1I1DP&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1611684425&oauth_version=1.0&oauth_signature=MEh5Rb72foVJtC6k8E9GsV6DeExMF%2BDyKK0CcRC4EeM%3D来自React Native应用程序的应用程序接口调用显示状态代码0,但没有响应。
当我复制生成的URL并从浏览器运行API (简单地将URL粘贴到地址栏中)时,调用成功,并且响应包含预期的数据。
但是,当我从终端cURL时,它抛出了401错误。
curl -H "accept:application/json" -H "user-agent:WooCommerce REST API - JS Client/1.0.1" http://localhost:8080/wp-json/wc/v3/products?oauth_consumer_key=ck_d1e9172ffce8f4016a3ae9b1ee8bce8b7a4c6365&oauth_nonce=ajJA9q13HYyZJU2TRMIqaeYU3vq1I1DP&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1611684425&oauth_version=1.0&oauth_signature=MEh5Rb72foVJtC6k8E9GsV6DeExMF%2BDyKK0CcRC4EeM%3D
{"code":"woocommerce_rest_authentication_missing_parameter","message":"Missing OAuth parameters oauth_timestamp, oauth_nonce, oauth_signature, oauth_signature_method","data":{"status":401}}到底怎么回事?为什么来自不同来源的反应如此不同?最重要的是,我如何在应用程序中获得响应?
发布于 2021-09-30 04:46:04
单词“localhost”是android的保留字。将localhost替换为您的pc的ip地址,它应该可以在android模拟器上正常工作。
发布于 2021-11-27 19:12:25
对于那些面临类似问题的人,一定要检查你的请求是在http上还是在https上。如果是http,则添加s。
https://stackoverflow.com/questions/65906963
复制相似问题