python-requests - 当我从 python 调用端点时返回 403 错误 - 我做错了什么?
问题描述
我已经注册并获得了 Walgreens 针对我的用户 ID 的商店定位器 API 密钥的所有详细信息。
当我在 python 中运行下面的代码(使用相关的私有凭据)时,我得到一个 403 代码,说 affId 丢失。
以下是来自 API 文档的示例代码 - 位于此处:https ://developer.walgreens.com/sites/default/files/v1_StoreLocatorAPI.html#SearchZipcode
request POST \
--url https://services-qa.walgreens.com/api/stores/search/v1 \
--header 'Content-Type: application/json' \
--data '{ \
"apiKey":"YOUR_API_KEY", \
"affId":"YOUR_AFFILIATE_ID", \
"zip": CUSTOMER_ZIPCODE, \
"r": RADIUS_MILES, \
"filterOptions": FILTER_OPTIONS_ARRAY, \
"requestType": "locator"
}
以下是我尝试使用相关参数创建对端点 URL 的调用,而不是 API 文档中我的 cred 代码:
Python代码
import requests
#define variables
urlws = 'https://services-qa.walgreens.com/api/stores/search/v1'
waid = 'USERID'
wapikey = 'APIKEY'
waslid = 'storesapi'
wazip = '60000' #store zip code
war = '25' #radius of store in miles
rtype = 'locator'
#header 'Content-Type: application/json'
data = {'affId': waid,
'apiKey': wapikey,
'r': war,
'zip': wazip,
'requestType': rtype}
r = requests.get(urlws, params=data)
r
收到响应 {“errCode”:“403”,“apiKey”:“”,“errMsg”:“密钥不存在”}
任何想法我做错了什么?
我是 python 和请求的新手。
TIA 寻求帮助。
解决方案
这是因为格式是 JSON,您必须使用帖子。一个例子。
import requests
response = requests.post('https://services-qa.walgreens.com/api/stores/search/v2', json={"apiKey":"Yourkey", "affId":"storesapi","address": "store address", "r":" 0", "s": "1", "requestType": "locator", "appVer":"1.0", "devInf":"DEVICE,##.#" })
print(response.url)
print(response.text)
推荐阅读
- python - 无法将字符串添加到 Python 中的字符串类型变量
- bokeh - Holoviews 活动工具配置和数据着色器布局
- file - 如何使用文件流 I/O 而不是“旧”I/O 来处理记录
- amazon-web-services - Kubernetes EKS、AWS CodeBuild 和 AWS Lambda
- postgresql - csv 文件没有空值,只有一些为零 (0) 的列值。但是当我从 csv 文件导入到 postgres 时,为什么它们看起来是空的,而不是零?
- php - 无法使用准备好的语句将 json 对象插入 mysql(没有工作)
- python - ImportError:使用 anaconda 安装后没有名为 pyautogui 的模块
- r - 是否有用于查找二元函数的局部最小值的 R 函数?
- python - 什么是 app_uuid?heroku
- r - 在 ggplot 中更改 stat_compare_mean() 输出的字体系列