Created
January 11, 2024 00:02
-
-
Save pinei/a0ce0bd12db8e36630b4f1defd96ae3e to your computer and use it in GitHub Desktop.
ANBIMA REST API
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
| 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