简体   繁体   中英

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

I am working on an Ionic 2 project for a woocommerce store. I am using Woocommerce REST API in my app and for testing the API with OAuth-1.0 using Postman Chrome App . I am getting proper responses with GET requests but for POST requests, I am getting error of signature mismatch, as:

{
  "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. The browser actually sends an OPTIONS request, which the woocommerce-api receives as GET. Using this tool helped with troubleshooting.

Finally solved it by setting my .htaccess as follows;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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