【问题标题】:oauth_problem=signature_invalid , Auoth 1.0 PUT requestoauth_problem=signature_invalid ,Auth 1.0 PUT 请求
【发布时间】:2021-02-03 02:17:04
【问题描述】:

我正在尝试使用 ETSY api 进行 api 调用以更新列表库存。

我收到此错误:oauth_problem=signature_invalid&debug_sbs=PUT

我的代码是:

const request = require('request')
const OAuth = require('oauth-1.0a')
const crypto = require('crypto')

let data ='produts data';


// Initialize
const oauth = OAuth({
    consumer: {
        key: api_key,
        secret: secret
    },
    signature_method: 'HMAC-SHA1',
    hash_function(base_string, key) {
        return crypto
            .createHmac('sha1', key)
            .update(base_string)
            .digest('base64')
    },
})
const request_data = {
    url: url,
    method: 'PUT',
    data: {
        products: data.products,
        price_on_property: [513],
        quantity_on_property: [513],
        sku_on_property: [513]
    },
}

// Note: The token is optional for some requests
const token = {
    key: access_token,
    secret: access_token_secret,
}
request({
    url: request_data.url,
    method: request_data.method,
    form: request_data.data,
    headers: oauth.toHeader(oauth.authorize(request_data, token)),
},
    function (error, response, body) {
        console.log(response.body)
    });

我的代码有什么遗漏吗?

【问题讨论】:

    标签: node.js oauth etsy


    【解决方案1】:

    Etsy API 要求 oauth 参数包含在帖子正文中,而不是标题中。文档不是很清楚,但有点暗示它(https://www.etsy.com/developers/documentation/getting_started/oauth#section_authorized_put_post_requests

    所以你的请求调用应该是这样的:

    request({
        url: request_data.url,
        method: request_data.method,
        form: oauth.authorize(request_data, token)
    },
    function (error, response, body) {
        console.log(response.body)
    });
    

    调试这类问题的好工具是 Postman (https://www.postman.com/)。这就是我能够追踪到这个问题的方式。

    编辑:

    如果请求在 Postman 中运行,但您仍然无法在代码中运行,那么您可以稍微中断该过程。通过调用 oauth 对象的 nonce、timestamp 和 signature 方法手动构建 oauth 参数。

    let oauth_data = {
        oauth_consumer_key: api_key,
        oauth_nonce: oauth.getNonce(),
        oauth_signature_method: "HMAC-SHA1",
        oauth_timestamp: oauth.getTimeStamp(),
        oauth_version: "1.0",
        oauth_token: access_token
     };
     //now generate the signature using the request data and the oauth object
     //you've just created above
     let signature = oauth.getSignature(request_data,token_secret,oauth_data);
     //now add the signature to the oauth_data paramater object
     oauth_data.oauth_signature = signature;
    
    
     //merge two objects into one
     let formData = Object.assign({},oauth_data,request_data.params);
    
     request({
        url: request_data.url,
        method: request_data.method,
        form: formData
     },
     function (error, response, body) {
        console.log(response.body)
     });
    

    我在我的代码中发现我在启动 oauth 对象时错误地命名了 consumer.secret 变量。所以消费者的秘密是空的,因此签名是错误的。

    另外,请尝试更简单的 PUT 调用,以测试您的基本 PUT 请求是否有效。我正在使用updateShop 更新商店名称(实际上您可以将其更新为现有名称,因此不会更改任何内容),只需要您的商店 ID 和标题字符串。

    【讨论】:

    • 我试过了,但还是不行
    • 好的,这是我开始使用 Postman 测试我的请求的时候。如果它适用于 Postman,则说明您的代码有问题。如果不是,则可能是您的请求有问题。
    • @Kamal_mod 我已经添加了更多代码,可以帮助您追踪事情
    猜你喜欢
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 2012-03-07
    • 2012-07-24
    • 1970-01-01
    相关资源
    最近更新 更多