基于Mozilla身份验证使用python发出API请求

基于Mozilla身份验证使用python发出API请求,python,api,rest,python-requests,Python,Api,Rest,Python Requests,我正在创建一个名为AnbimaApiClient的类,旨在促进对anbimaapi的数据请求(API文档链接:)。我成功地实现了一个名为\u get\u new\u anbima\u token的方法,该方法为api调用生成一个访问令牌。问题出在get\u fidc\u方法中。此功能的目标是根据unioque识别码(称为CNPJ)提取巴西对冲基金的数据。(以下是API如何提取对冲基金数据的文档链接:) 代码 问题 每次运行此脚本时,这都是响应的输出。text: Could not find a

我正在创建一个名为
AnbimaApiClient
的类,旨在促进对anbimaapi的数据请求(API文档链接:)。我成功地实现了一个名为
\u get\u new\u anbima\u token
的方法,该方法为api调用生成一个访问令牌。问题出在
get\u fidc\u方法中。此功能的目标是根据unioque识别码(称为CNPJ)提取巴西对冲基金的数据。(以下是API如何提取对冲基金数据的文档链接:)

代码 问题 每次运行此脚本时,这都是
响应的输出。text

Could not find a required APP in the request, identified by HEADER client_id.

我知道API遵循此身份验证过程,因为它是API文档中编写的:“身份验证类型必须为“基本身份验证”,如中所述:我已尝试在标头中以身份验证的形式传递客户端\u id,但仍然无效。你们能帮帮我吗?我到底做错了什么?标题里有什么?非常欢迎提供任何帮助,谢谢大家的耐心。

根据Anbima的网站,身份验证应在base64:

“根据em base 64中的信息,通过campo授权进行编码。AASM,PARA GEAR O标题授权做PAR clitTydID=AC2YAAC23 e秘密=1BHS45 TT,POR示例,deve ser gerada A BASE64 DA串AC2YAAC23:1BHS45 TT,QueRealTaulaNa CHAVY YUMYYWHZIZOJAFFAFM0NVRU。Desta forma,o header ficaria:“授权”:“基本Yumeywfhyzizojfiafm0nvru”

尝试使用base64库:

import requests, json, base64

aut_ascii = 'client_id:client_secret' # for example "aC2yaac23:1bhS45TT"
message_bytes = aut_ascii.encode('ascii')
message_64 = base64.b64encode(message_bytes).decode('ascii')

header_aut = {"Content-Type":"application/json",
             "Authorization":"Basic %s"%message_64
            }
data_aut = {"grant_type":"client_credentials"}

aut = requests.post(url="https://api.anbima.com.br/oauth/access-token",headers=header_aut,data=json.dumps(data_aut),allow_redirects=True)

print(aut.content)
import requests, json, base64

aut_ascii = 'client_id:client_secret' # for example "aC2yaac23:1bhS45TT"
message_bytes = aut_ascii.encode('ascii')
message_64 = base64.b64encode(message_bytes).decode('ascii')

header_aut = {"Content-Type":"application/json",
             "Authorization":"Basic %s"%message_64
            }
data_aut = {"grant_type":"client_credentials"}

aut = requests.post(url="https://api.anbima.com.br/oauth/access-token",headers=header_aut,data=json.dumps(data_aut),allow_redirects=True)

print(aut.content)