Created
June 2, 2023 13:06
-
-
Save seagalputra/2916db4809e172c186d53f4da7cbce76 to your computer and use it in GitHub Desktop.
WooCommerce API with Fetch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import OAuth from "oauth-1.0a"; | |
import crypto from "crypto"; | |
const authParams = { | |
consumer: { | |
key: "<consumer-key>", | |
secret: "<consumer-secret>", | |
}, | |
signature_method: "HMAC-SHA256", | |
hash_function: (base: any, key: any) => { | |
return crypto.createHmac("sha256", key).update(base).digest("base64"); | |
}, | |
}; | |
const oauth = new OAuth(authParams); | |
const url = "http://localhost:10004/wp-json/wc/v3/products"; | |
const auth = oauth.authorize({ | |
url, | |
method: "get", | |
}); | |
const authHeader = oauth.toHeader(auth); | |
const res = await fetch(url, { | |
headers: { | |
...authHeader, | |
}, | |
}); | |
const products = await res.json(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment