首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将CURL请求转换为Axios

将CURL请求转换为Axios
EN

Stack Overflow用户
提问于 2021-09-18 04:04:00
回答 1查看 381关注 0票数 0

请告诉我如何将这个curl请求转换为它的axios等效项?

代码语言:javascript
运行
复制
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/'

我在这上面花了几个小时,只是为了让我能继续

代码语言:javascript
运行
复制
 {"code":1,"message":"Invalid request body"}

这是我尝试过的:

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

我将非常感谢任何帮助,因为这已经浪费了我太多的时间。我现在赶不上截止日期了.谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-18 04:10:25

您需要urlencode参数,而不是将对象作为数据传递。

代码语言:javascript
运行
复制
const params = new URLSearchParams();
params.append('redirect_uri', process.env.PINTEREST_OAUTH_CALLBACK_URL)
// do the same for other parameters
// ...
// make post
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69231482

复制
相关文章

相似问题

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