Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
Making post requests to Pinterest APIs using Axios

I am currently stucked on making post requests to pinterest api. While I feel my code is working well, I keep getting back "Invalid Request Body" from the pinterest api.

Based on the documentation on their site, here is how they said the request should be made:

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/'

Here is how I made the request using Axios from my ExpressJS application:

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

Here is the response I keep getting back:

{code:1, message:"Invalid request body"}

Here is the link to the documentation I am working with

https://developers.pinterest.com/docs/api/v5/#tag/Authentication

Please, what could I be doing wrong here, because I believed I provided everything they needed in the request.

Thanks in anticipation for your input.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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)}`);
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda