首页 > 解决方案 > 当我从 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 寻求帮助。

标签: python-requestspython-3.6

解决方案


这是因为格式是 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)

推荐阅读