首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >oauth_problem=signature_invalid,Auoth 1.0 PUT请求

oauth_problem=signature_invalid,Auoth 1.0 PUT请求
EN

Stack Overflow用户
提问于 2021-01-23 08:34:46
回答 1查看 336关注 0票数 1

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

我得到了这个错误: oauth_problem=signature_invalid&debug_sbs=PUT

我的代码是:

代码语言:javascript
复制
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)
    });

我的代码中是否遗漏了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-01-24 17:12:39

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

因此,您的请求调用应如下所示:

代码语言:javascript
复制
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参数。

代码语言:javascript
复制
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)
 });

所以消费者的秘密是空的,因此签名是错误的。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65854567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档