【发布时间】:2021-11-12 12:08:30
【问题描述】:
我目前坚持向 pinterest api 发出帖子请求。虽然我觉得我的代码运行良好,但我不断从 pinterest api 回复 "Invalid Request Body"。
根据他们网站上的文档,这是他们所说的应该提出请求的方式:
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/'
这是我在 ExpressJS 应用程序中使用 Axios 发出请求的方式:
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: encodeURIComponent(code), //code is defined at the top as received from pinterest
grant_type: encodeURIComponent("authorization_code"),
redirect_uri: encodeURIComponent(redirectUrl)
}
console.log(`RequestBody =${JSON.stringify(accessTokenRequestBody, null, 2)}`);
const clientIdAndSecretBase64 = Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
axios.post(url, accessTokenRequestBody, {
headers: {
'Content-Type': 'application/application/x-www-form-urlencoded; charset=UTF-8',
'Authorization': `Basic ${clientIdAndSecretBase64}`
}
}).then((response) => {
let responseData = response.data;
console.log(`Pinterest ResponseData = ${JSON.stringify(responseData, null, 2)}`);
}).catch((e) => {
console.log(`${JSON.stringify(e.response.data)}`);
});
这是我不断收到的回复:
{code:1, message:"Invalid request body"}
这是我正在使用的文档的链接
https://developers.pinterest.com/docs/api/v5/#tag/Authentication
拜托,我在这里做错了什么,因为我相信我在请求中提供了他们需要的一切。
感谢您的期待。
【问题讨论】:
标签: javascript node.js express axios pinterest