簡體   English   中英

使用 Axios 向 Pinterest API 發出發布請求

[英]Making post requests to Pinterest APIs using Axios

我目前堅持向 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/'

以下是我如何使用 Axios 從我的 ExpressJS 應用程序發出請求:

    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

拜托,我在這里做錯了什么,因為我相信我在請求中提供了他們需要的一切。

感謝您的投入。

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 body = {
    'code': code, //code is defined at the top as received from pinterest
    'grant_type': 'authorization_code',
    'redirect_uri': redirectUrl
};

// Fix code
let accessTokenRequestBody = Object.keys(body)
    .map(k => `${encodeURIComponent(k)}=${encodeURIComponent(body[k])}`)
    .join('&');

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)}`);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM