使用需要不记名令牌的 API 在 Python 中进行 API 调用 [英] Making an API call in Python with an API that requires a bearer token

查看:38
本文介绍了使用需要不记名令牌的 API 在 Python 中进行 API 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻求将 JSON API 调用集成到 Python 程序的帮助.

Looking for some help with integrating a JSON API call into a Python program.

我希望将以下 API 集成到 Python .py 程序中,以允许调用它并打印响应.

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.

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"和requests"路由,但无济于事.

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 详细信息:IBM X-Force Exchange API 文档 - IP 声誉

Full API details: IBM X-Force Exchange API Documentation - IP Reputation

推荐答案

这只是意味着它希望它作为标题数据中的一个键

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())

这篇关于使用需要不记名令牌的 API 在 Python 中进行 API 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆