首页 > 解决方案 > 如何从具有帐户密钥的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_KEYrequests.get,比如用户名和密码

标题中的示例 headers = {'Content-type': 'application/json','X-IG-API-KEY':'0x-x-x-x0'}

标签: pythonapi

解决方案


这是你想要的 ?

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)

参考:使用 Python 中的 requests 包使用 API 密钥调用 REST API


推荐阅读