我正在将请求发送到Ticketmaster Discovery v2 API。我已经使用将API密钥嵌入URL中的方法发送了请求并收到了所需的响应。但是,当我更改代码以使用授权标头而不是将其嵌入URL时,代码将完全停止工作并返回错误。
我是API的新手,Ticketmaster的API文档在身份验证标头的语法方面相对较少,因此,对我的代码进行第二次(更老练)的研究可能有助于发现我的错误。
我很好奇我是否在代码中语法上做错了什么,导致缺少期望的响应。
如果不是我的语法问题,那么我很想听听其他可能发现问题的想法或故障排除方法。
我试图在第一段和第二段代码之间进行尽可能小的更改。除了嵌入式API密钥外,它们似乎都生成相同的URL字符串。因此,我相信我的标头语法可能是罪魁祸首,但是我缺乏确定的REST API经验。
import requests
import json
import sqlite3
from datetime import datetime
import keys
def date(datestr, format = "%Y-%m-%d %H:%M:%S"): # a function to print out the date/time
# of the event in a nice way
d = datetime.strptime(datestr, format)
pretty = d.strftime("%a, %b %d, %Y at %H:%M")
return pretty
ticketmaster_key = keys.ticketmaster_key #assign ticketmaster key from keys file
CACHE_FNAME = "ticketmaster_cache.json" #create a cache for ticketmaster events
try:
cache_file = open(CACHE_FNAME, "r") #try to read file
cache_contents = cache_file.read() #if it's there, read it as a string
CACHE_DICTION = json.loads(cache_contents) #load it as a dictionary
cache_file.close() #close file
except:
CACHE_DICTION = {} #Creates empty dictionary object called CACHE_DICTION
def get_event_info(search, ticketmaster_key = ticketmaster_key): #function to get event info from ticketmaster
#it either gets from the cache if it's there or
if search in CACHE_DICTION: #requests from the API
d = CACHE_DICTION[search]
else: #the params for the URL include the search, key,
data = requests.get("https://app.ticketmaster.com/discovery/v2/events", #format, dmaId which is the New York code, size of response
params = {"keyword": search, "apikey": ticketmaster_key, #sends GET request to ticketmaster.com website
"format":"json", "dmaId": "366", "size": 200, "radius": "2"})
print(data.url)
d = json.loads(data.text) #takes text from "data" variable and saves as a json object named d
CACHE_DICTION[search] = d #saves json object "d" into cache dictionary
f = open(CACHE_FNAME, 'w') #Opens cache with filename CACHE_FNAME for writing.
f.write(json.dumps(CACHE_DICTION)) #Writes json object called CACHE_DICTION to CACHE_FNAME
f.close() #Closes cache file called CACHE_FNAME
return d
ticket_search = get_event_info("") #search Ticketmaster for keyword contained in quotes
print(ticket_search)
import requests
import json
import sqlite3
from datetime import datetime
# Variable to store API token for ticketmaster API
api_token = "g4uj38J6W2KKNGizVchZ3mgdwkOgIXJr"
# Variable to store base url for API string
api_url_base = "https://app.ticketmaster.com/discovery/v2/"
# HTTP header
headers = {'Content-Type':'application/json','Authorization':'Bearer {0}'.format(api_token)}
# a function to print out the date/time in a pretty way
def date(datestr, format="%Y-%m-%d %H:%M:%S"):
d = datetime.strptime(datestr, format)
pretty = d.strftime("%a, %b %d, %Y at %H:%M")
return pretty
# Create json file for caching and store in local variable
CACHE_FNAME = "ticketmaster_cache.json"
try:
# Open cache file for reading and assign file pointer
cache_file = open(CACHE_FNAME, "r")
# Read contents of cache file and store in local variable
cache_contents = cache_file.read()
# Load contents to dictionary object
CACHE_DICTION = json.loads(cache_contents)
# Close file pointed to by file pointer
cache_file.close()
# Create new empty dictionary if none existed previously
except:
CACHE_DICTION = {}
def get_event_info(search):
if search in CACHE_DICTION:
d = CACHE_DICTION[search]
else:
api_url = '{0}events'.format(api_url_base)
payload = {"keyword": search, "dmaId": "366", "size": 200, "radius": "2"}
data = requests.get(api_url, headers=headers, params=payload)
print(data.url)
d = json.loads(data.text)
CACHE_DICTION[search] = d
f = open(CACHE_FNAME, 'w')
f.write(json.dumps(CACHE_DICTION))
f.close()
return d
ticket_search = get_event_info("music")
print(ticket_search)
当我运行使用基于标头的身份验证的代码段时,生成的URL字符串如下所示:
https://app.ticketmaster.com/discovery/v2/events?keyword=music&dmaId=366&size=200&radius=2
返回的错误是这样的:
{'fault':{'faultstring':'无法解析API密钥变量request.queryparam.apikey','detail':{'errorcode':'steps.oauth.v2.FailedToResolveAPIKey'}}} >