简体   繁体   English

Woocommerce在帖子请求上休息api无效签名(错误401)

[英]Woocommerce rest api invalid signature (error 401) on post request

I am working on an Ionic 2 project for a woocommerce store. 我正在为一家woocommerce商店开展Ionic 2项目。 I am using Woocommerce REST API in my app and for testing the API with OAuth-1.0 using Postman Chrome App . 我在我的应用程序中使用Woocommerce REST API ,并使用Postman Chrome App在OAuth-1.0上测试API。 I am getting proper responses with GET requests but for POST requests, I am getting error of signature mismatch, as: 我正在使用GET请求获得正确的响应,但对于POST请求,我收到签名不匹配的错误,如:

{
  "code": "woocommerce_rest_authentication_error",
  "message": "Invalid Signature - provided signature does not match.",
  "data": {
    "status": 401
  }
}

I struggled with this for a few days (using angular) and finally figured out that it was a CORS issue. 我挣扎了几天(使用角度),最后发现这是一个CORS问题。 The browser actually sends an OPTIONS request, which the woocommerce-api receives as GET. 浏览器实际上发送了一个OPTIONS请求,woocommerce-api收到该请求作为GET。 Using this tool helped with troubleshooting. 使用此工具有助于排除故障。

Finally solved it by setting my .htaccess as follows; 最后通过如下设置我的.htaccess来解决它;

RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L,E=HTTP_ORIGIN:%{HTTP:ORIGIN}]]
Header set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Credentials "true"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"

You can refer to this answer for a detailed explanation 您可以参考此答案以获取详细说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM