Skip to content

Instantly share code, notes, and snippets.

@diegomdrs
Last active January 10, 2022 16:00
Show Gist options
  • Save diegomdrs/86962bbe998b8e5e067aa464b628e471 to your computer and use it in GitHub Desktop.
Save diegomdrs/86962bbe998b8e5e067aa464b628e471 to your computer and use it in GitHub Desktop.
Google Script Nubank Statement

Google Apps Script Nubank Statement

Obtém o extrato (aberto e futuro) do cartão de crédito no formato json a partir dos endpoints disponíveis na versão web do Nubank, utilizando RESTful API com o Google Apps Script

Obtendo os ids dos extratos abertos e futuros

Para obter os ids dos extratos aberto e futuro a partir de um arquivo .har, execute:

jq '[.log.entries[] | select(._resourceType == "xhr" and .response.content.mimeType == "application/json")] | [{ content: .[].response.content.text }] | [[{ json: .[].content | fromjson }][] | select(.json.bill | not)] | map(.json._links)' /tmp/conta.nubank.com.br.har

Obtendo o token JWT

Para obter o token JWT para acesso ao extrato, a partir de um arquivo .har, execute:

jq '[.log.entries[].request.headers[] | select(.name == "Authorization")] | map({auth: .value})' /tmp/conta.nubank.com.br.har
const STATEMENT_PATH = 'https://prod-global-webapp-proxy.nubank.com.br/api/proxy'
const OPEN_STATEMENT_ID = ''
const FUTURE_STATEMENTS_ID = ''
const TOKEN_JWT = ''
function getOpenStatementItems() {
return getItems(OPEN_STATEMENT_ID)
}
function getFutureStatementItems() {
return getItems(FUTURE_STATEMENTS_ID)
}
function getItems(statementId) {
const options = {
muteHttpExceptions: true,
contentType: 'application/json; charset=UTF-8',
headers: {
'Authorization': `Bearer ${TOKEN_JWT}`
}
}
const response = UrlFetchApp.fetch(`${STATEMENT_PATH}/${statementId}`, options)
const responseCode = response.getResponseCode()
const contentText = response.getContentText()
Logger.log(`Get Items Response Code: '${responseCode}'`)
if (responseCode === 200) {
const json = JSON.parse(response.getContentText())
return json
} else {
const jsonError = parseJSON(contentText)
const errorMessage = typeof jsonError === 'object' ? jsonError.error : contentText;
throw new Error(`Get Items Error: ${errorMessage}`)
}
}
function parseJSON(str) {
try {
return JSON.parse(str)
} catch (e) {
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment