如何在Node.js的axios中发送标头?

时间:2019-05-03 16:23:08

标签: oauth-2.0 axios ebay

我正在尝试进行ebay Oauth,并且使用纯Node.js遇到了axios的问题。我一生无法发送适当的标头,因为它不断返回error_description: 'grant type in request is not supported by the authorization server'

router.get('/', function(req, res) {
let client = "client_id";
let secret = "secretServerKey";
let scopeStr = qs.escape('https://api.sandbox.ebay.com/oauth/api_scope')
// console.log(Buffer(client+":"+secret).toString('base64'))
// console.log(scopeStr)
axios.post('https://api.sandbox.ebay.com/identity/v1/oauth2/token',

  grant_type: "client_credentials",
scope:  scopeStr },

{
  headers: {
  'Content-Type': 'application/x-www-form-urlencoded',
  'Authorization': 'Basic '+ Buffer(client+":"+secret).toString('base64')
}
}).then( tokenRes =>{

console.log(tokenRes)
res.send({result: "we've downloaded all active listings from category_id 1!" });
}).catch(e=>{
console.log(e.response.status)
console.log(e.response.statusText)
console.log(e.response.data)

})

我尝试执行另一种授予类型:“ authorization_code”,但仍收到相同的结果。使用Curl,并使用适当的base64加密的clientId:secret可以正常工作……

curl -X POST 'https://api.sandbox.ebay.com/identity/v1/oauth2/token' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Authorization: Basic 8u234820randomlongsjklfjj2uhjsdf=' \
  -d 'grant_type=client_credentials&scope=https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope'

edit1:好的,可以肯定的是,我了解到我不得不覆盖默认的授权标头,这很可能会解决我的问题,但是这让我挠头,因为现在我发现我的标头没有通过请求符合预期。

0 个答案:

没有答案