Pagseguro API 401 error - Unauthorized; Error logging in paid

Asked

Viewed 253 times

0

[RESOLVED] I made a simple Node server to test the Pagseguro API and am having problems with error 401. In a video I saw that to make requests for the Pagseguro API it is necessary to have an SSL certificate, so I deploy to Heroku

inserir a descrição da imagem aqui
As in the image above, the lock is closed and the URL starts with https:// which means that the address has an SSL certificate, but even so the API is giving error.
I have tried to solve this problem in a number of ways, including using Cors-Anywhere both online and locally and it also didn’t work.
I made a request using the desktop application Postman and in it the request returned status 200 inserir a descrição da imagem aqui In the Postman request I did not set any header, I just selected the option x-www-form-urlencoded and put the information [email, token]

Information:

  • Credentials [email, token] are correct
  • deploy contains no errors

Below is the Node server code:

require('dotenv').config()
const fetch = require('node-fetch')
const Headers = fetch.Headers
const express = require('express')

const app = express()

app.get('/', (req, res) => {
  res.send('working pgs')
})

app.get('/teste', async (req, res) => {
  const r = await fetch('https://ws.sandbox.pagseguro.uol.com.br/sessions/', {
    method: 'post',
    headers: new Headers({"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"}),
    body: {
      email: encodeURIComponent(process.env.PGS_EMAIL),
      token: encodeURIComponent(process.env.PGS_TOKEN)
    }
  })
  const t = await r.text()
  console.log(r)
  res.send(`teste ${JSON.stringify(r)}`)
})

app.listen(process.env.PORT || 3333)

I hope I have synthesized my doubt in an easy way to be understood if by chance some information is missing please let me know that I put here.

Link to the Heroku: https://node-deploy-pgs.herokuapp.com/teste

  • There is no need to indicate in the text of the question that the problem has been solved. Just accept an answer that we will already know.

1 answer

0


That body you put on is totally wrong:

body: {
  email: encodeURIComponent(process.env.PGS_EMAIL),
  token: encodeURIComponent(process.env.PGS_TOKEN)
}

That’s not how the fetch() works, if it is x-www-form-urlencoded use the URLSearchParams, just like that (and you DON’T need the encodeURIComponent, the URLSearchParams already fix it):

const fetch = require('node-fetch');
const { URLSearchParams } = require('url');

...

const r = await fetch('https://ws.sandbox.pagseguro.uol.com.br/sessions/', {
    method: 'post',
    body: new URLSearchParams({
      email: process.env.PGS_EMAIL,
      token: process.env.PGS_TOKEN
    })
})

As the very documentation of the module you installed suggests:

You can also use the module (installed the part https://www.npmjs.com/package/form-data) form-data, as shown in the example:

const fetch = require('node-fetch');
const FormData = require('form-data');

...

const form = new FormData();

form.append('email', process.env.PGS_EMAIL);
form.append('token', process.env.PGS_TOKEN);

const r = await fetch('https://ws.sandbox.pagseguro.uol.com.br/sessions/', {
    method: 'post',
    body: form
})

No need to set up the header, the bodys x-www-form-urlencoded and form-data are "natural", so internally the headers are "solved".

  • It worked, thank you very much, I’m still learning how to make http requests

Browser other questions tagged

You are not signed in. Login or sign up in order to post.