将 CURL 请求转换为 Axios
Convert CURL Request to Axios
请问如何将这个 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)}`);
});
我真的很感激任何帮助,因为这浪费了我很多时间。我目前 运行 落后于截止日期...谢谢。
您需要 urlencode 参数而不是将对象作为数据传递。
const params = new URLSearchParams();
params.append('redirect_uri', process.env.PINTEREST_OAUTH_CALLBACK_URL)
// do the same for other parameters
// ...
// make post
请问如何将这个 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)}`);
});
我真的很感激任何帮助,因为这浪费了我很多时间。我目前 运行 落后于截止日期...谢谢。
您需要 urlencode 参数而不是将对象作为数据传递。
const params = new URLSearchParams();
params.append('redirect_uri', process.env.PINTEREST_OAUTH_CALLBACK_URL)
// do the same for other parameters
// ...
// make post