【问题标题】:Making post requests to Pinterest APIs using Axios使用 Axios 向 Pinterest API 发出 post 请求
【发布时间】: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


    【解决方案1】:
    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)}`);
    });
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2020-11-05
    • 2020-09-17
    • 1970-01-01
    • 2022-11-02
    • 2020-05-23
    • 2019-08-22
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多