在javascript / typescript中生成OAuth签名

时间:2018-06-16 09:30:44

标签: woocommerce oauth ionic2 cryptojs woocommerce-rest-api

我需要帮助为woocommerce创建oauth签名。我跟随他们的文档,https://woocommerce.github.io/woocommerce-rest-api-docs/#authentication-over-http使用CryptoJS,但仍然无效。看起来我做错了什么。我一直收到一个无效的签名错误,他们只是不给我任何回复,但继续向我推荐没有适当样本帮助的文档。有人可以帮忙吗?

这是我目前使用javascript / typescript的方式。

export class AppAuth {
    readonly oauth_consumer_key: string;
    readonly oauth_nonce: string;
    private _oauth_signature: string;
    readonly oauth_signature_method: string;
    readonly oauth_timestamp: string;

    constructor() {
        this.oauth_consumer_key = CONSUMER_KEY;
        this.oauth_nonce = Math.round(Math.random() * Math.pow(10, 8)) + "";
        this.oauth_signature_method = "HMAC-SHA1";
        this.oauth_timestamp = Math.round(new Date().getTime() / 1000) + "";
    }

    getOAuth_signature(url: string): string {
        let fEncString = "GET&" + encodeURIComponent(url);
        let parameterString = "oauth_consumer_key=" + this.oauth_consumer_key + "&oauth_nonce=" + this.oauth_nonce + "&oauth_signature_method=" + this.oauth_signature_method + "&oauth_timestamp=" + this.oauth_timestamp + "&oauth_version=1.0";
        let sEncString = "&" + encodeURIComponent(parameterString);
        let baseString = fEncString + sEncString;

        console.log("Parameters: ", sEncString);
        console.log("URL ENC: ", fEncString);
        console.log("Signature String: ", baseString);

        let hash = CryptoJS.HmacSHA1(baseString, (CONSUMER_SECRET + "&"));
        let signature = CryptoJS.enc.Base64.stringify(hash)
        return signature;
    }
}

该网址正在获取单个产品的详细信息。例如。 http://somesite.com/product/244其中244是产品ID,是动态生成的。在生成签名时,我应该使用带有id或没有id的url。提前谢谢。

0 个答案:

没有答案