Skip to content

Instantly share code, notes, and snippets.

@hudsantos
Last active January 13, 2024 01:20
Show Gist options
  • Save hudsantos/73700d33060068cfa9f1c336bf6d41bc to your computer and use it in GitHub Desktop.
Save hudsantos/73700d33060068cfa9f1c336bf6d41bc to your computer and use it in GitHub Desktop.
Como conseguir o token do Banco do Brasil usando JavaScript (nodejs)
// Em BASH puro, usando cURL ficaria assim:
// curl --basic --request POST \
// --url "https://oauth.hm.bb.com.br/oauth/token/?grant_type=client_credentials&scope=cobranca.registro-boletos" \
// --header 'Authorization: Basic your_top_secret_base64_encoded_credentials==' \
// --header 'Content-Type: application/x-www-form-urlencoded' \
// --header 'cache-control: no-cache'
// Thanks to: http://andreybleme.com/2017-05-27/como-funciona-o-protocolo-oauth-20/
const https = require('https');
const auth_64='your_top_secret_base64_encoded_credentials=='
const options = {
hostname: 'oauth.hm.bb.com.br',
port: 443,
path: '/oauth/token/?grant_type=client_credentials&scope=cobranca.registro-boletos',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic '+auth_64,
'cache-control': 'no-cache'
}
};
https.get(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
}).on('error', (e) => {
console.error(e);
});
@rafael-deroncio
Copy link

@hudsantos Como você gerou o conteúdo da authorization? Mesclou ClientId com ClientSecret e passou para Base64?

@hudsantos
Copy link
Author

Exatamente, mas concatena separado por : (dois pontos). Geralmente a string em base64 termina com ==.
Exemplo pra gerar uma a credencial codificada em base64 através do bash:

$ echo "ClientId:ClientSecret" | base64
Q2xpZW50SWQ6Q2xpZW50U2VjcmV0Cg==

Logicamente substituindo pelo seu ClientId e ClientSecret.

Vlw!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment