请告诉我如何将这个curl请求转换为它的axios等效项?
curl -X POST https://api.pinterest.com/v5/oauth/token --header "Authorization: Basic {base64 encoded string made of client_id:client_secret}" --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'grant_type=authorization_code' --data-urlencode 'code={YOUR_CODE}' --data-urlencode 'redirect_uri=http://localhost/'
我在这上面花了几个小时,只是为了让我能继续
{"code":1,"message":"Invalid request body"}
这是我尝试过的:
const redirectUrl = process.env.PINTEREST_OAUTH_CALLBACK_URL;
const clientId = process.env.PINTEREST_APP_ID;
const clientSecret = process.env.PINTEREST_APP_SECRET;
let url = 'https://api.pinterest.com/v5/oauth/token';
let accessTokenRequestBody = {
code: code,
grant_type: "authorization_code",
redirect_uri: redirectUrl
};
const clientIdAndSecretBase64 = Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
axios(url, {
method: 'post',
url: url,
data: accessTokenRequestBody,
headers: {
"Content-Type": 'application/application/x-www-form-urlencoded; charset=UTF-8',
"Authorization": `Basic ${clientIdAndSecretBase64}`
}
}).then((response) => {
let responseData = response.data;
let accessToken = module.exports.encodePayloadIntoJWT(responseData);
console.log(`Pinterest ResponseData = ${JSON.stringify(responseData, null, 2)}`);
}).catch((e) => {
console.log(`${JSON.stringify(e.response.data)}`);
});
我将非常感谢任何帮助,因为这已经浪费了我太多的时间。我现在赶不上截止日期了.谢谢。
发布于 2021-09-18 04:10:25
您需要urlencode参数,而不是将对象作为数据传递。
const params = new URLSearchParams();
params.append('redirect_uri', process.env.PINTEREST_OAUTH_CALLBACK_URL)
// do the same for other parameters
// ...
// make post
https://stackoverflow.com/questions/69231482
复制相似问题