python - 如何从具有帐户密钥的api获取请求
问题描述
import requests
import json
def test_request_response():
# Send a request to the API server and store the response.
data = { 'username' : 'sxxxt@xxx.com', 'password' : 'xxxxe' }
headers = {'Content-type': 'application/json'}
response = requests.get('https://xx.xxx.com/xx/xx',headers=headers,data=json.dumps(data))
return response
test_request_response()
输出
我有一个 api 密钥API_KEY = '0x-x-x-x0
,还有什么我可以提供API_KEY
的requests.get
,比如用户名和密码
标题中的示例 headers = {'Content-type': 'application/json','X-IG-API-KEY':'0x-x-x-x0'}
解决方案
这是你想要的 ?
from requests.auth import HTTPBasicAuth
import requests
url = "https://api_url"
headers = {"Accept": "application/json"}
auth = HTTPBasicAuth('apikey', '1234abcd')
files = {'filename': open('filename','rb')}
req = requests.get(url, headers=headers , auth=auth , files=files)
推荐阅读
- java - Project Reactor 中的 Akka 流 .via() 等价物是什么?
- docker - Apache虚拟主机如何添加带有标题的位置?
- java - 'java' 不是内部或外部命令、可运行程序或批处理文件
- javascript - 如何更改此函数执行顺序
- javascript - 如何在 Discord.js 中使用消息更改用户的昵称?
- python - Pytorch NCCL DDP 冻结但 Gloo 工作
- javascript - JS中使用类的多表单验证
- java - 如何在测试执行期间将参数值注入bean?
- javascript - AgGrid:恢复状态正在向右移动自动组列
- reactjs - 如何在 MapStateToProps 中使用来自 URL 的查询参数