Last active
August 2, 2024 13:42
-
-
Save hudsantos/73700d33060068cfa9f1c336bf6d41bc to your computer and use it in GitHub Desktop.
Como conseguir o token do Banco do Brasil usando JavaScript (nodejs)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); |
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
@hudsantos Como você gerou o conteúdo da authorization? Mesclou ClientId com ClientSecret e passou para Base64?