在Python中使用需要承载令牌的API调用API [英] Making an API call in Python with an API that requires a bearer token
问题描述
寻找将JSON API调用集成到python程序中的一些帮助。
Looking for some help with integrating a JSON API call into a python program.
我希望将以下API集成到python .py程序中,要调用和要打印的响应: https://xforce-api.mybluemix .net:443 / ipr / 8.8.8.8
I am looking to integrate the following API into a python .py program to allow it to be called and the response to be printed: https://xforce-api.mybluemix.net:443/ipr/8.8.8.8
API指南规定必须生成承载令牌,以允许调用API成功完成。但是我不确定在Python API请求中将此令牌作为承载令牌身份验证的语法。
The API guidance states that a bearer token must be generated to allow calls to the API, which I have done successfully. However I am unsure of the syntax to include this token as bearer token authentication in Python API request.
我可以使用包含令牌的cURL成功完成上述请求。我尝试了urllib和请求路由,但无效。
I can successfully complete the above request using cURL with a token included. I have tried "urllib" and "requests" routes but to no avail.
完整的API详细信息位于此链接: https://xforce-api.mybluemix.net/doc/#!/IP_Reputation/ipr_ip_get
Full API details are at this link: https://xforce-api.mybluemix.net/doc/#!/IP_Reputation/ipr_ip_get
推荐答案
它只是意味着它作为标题数据中的键
It just means it expects that as a key in your header data
import requests
endpoint = ".../api/ip"
data = {"ip":"1.1.2.3"}
headers = {"Authorization":"Bearer MYREALLYLONGTOKENIGOT"}
print requests.post(endpoint,data=data,headers=headers).json()
这篇关于在Python中使用需要承载令牌的API调用API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!