Skip to content

Instantly share code, notes, and snippets.

@pinei
Created January 11, 2024 00:02
Show Gist options
  • Select an option

  • Save pinei/a0ce0bd12db8e36630b4f1defd96ae3e to your computer and use it in GitHub Desktop.

Select an option

Save pinei/a0ce0bd12db8e36630b4f1defd96ae3e to your computer and use it in GitHub Desktop.
ANBIMA REST API
import requests
import json
import base64
# Unusable codes
CLIENT_ID = 'EbYD8dKZ17w0'
AUT_ASCII = "EbYD8dKZ17w0:b1awDkIkqhqg"
URL_OAUTH = "https://api.anbima.com.br/oauth/access-token"
URL = "https://api.anbima.com.br"
def request_key():
message_bytes = AUT_ASCII.encode('ascii')
message_64 = base64.b64encode(message_bytes).decode('ascii')
header_aut = {
"Content-type": "application/json",
"Authorization": f"Basic {message_64}"
}
data_aut = {
"grant_type": "client_credentials"
}
aut = requests.post(url=URL_OAUTH, headers=header_aut,
data=json.dumps(data_aut), allow_redirects=True)
return aut.content
def get_fund_info(access_token, codigo_fundo):
header_get = {
"Content-type": "application/json",
"client_id": CLIENT_ID,
"access_token": access_token
}
url = f'{URL}/feed/fundos/v1/fundos/{codigo_fundo}/serie-historica'
print(url)
session = requests.Session()
response = session.get(url=url, headers=header_get)
return response.text
response = request_key()
# Get the value of the "access_token" field
access_token = json.loads(response.decode('utf-8'))['access_token']
print(access_token)
print(get_fund_info(access_token, '714712'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment