我正在尝试建立Marvel API调用。
以下是有关授权的链接: https://developer.marvel.com/documentation/authorization
我正在尝试创建服务器端应用程序,因此根据上面的链接,我需要一个时间戳,apikey和哈希URL参数。哈希必须是以下形式的md5哈希:md5(timestamp + privateKey + publicKey),而apikey url param是我的公共密钥。
这是我的代码,我正在Python 3中发出请求,使用请求库形成请求,使用时间库形成时间戳,并使用hashlib库形成哈希。
#request.py: making a http request to marvel api
import requests;
import time;
import hashlib;
#timestamp
ts = time.time();
ts_str = str(float(ts));
#keys
public_key = 'a3c785ecc50aa21b134fca1391903926';
private_key = 'my_private_key';
#hash and encodings
m_hash = hashlib.md5();
ts_str_byte = bytes(ts_str, 'utf-8');
private_key_byte = bytes(private_key, 'utf-8');
public_key_byte = bytes(public_key, 'utf-8');
m_hash.update(ts_str_byte + private_key_byte + public_key_byte);
m_hash_str = str(m_hash.digest());
#all request parameters
payload = {'ts': ts_str, 'apikey': 'a3c785ecc50aa21b134fca1391903926', 'hash': m_hash_str};
#make request
r = requests.get('https://gateway.marvel.com:443/v1/public/characters', params=payload);
#for debugging
print(r.url);
print(r.json());
以下是输出:
我不确定是什么原因导致组合无效。
我可以根据要求提供更多信息。任何信息,将不胜感激。谢谢!
编辑:
一般来说,我对API调用还是有点陌生。是否有任何资源可以帮助您更多地了解如何执行它们?到目前为止,以我有限的经验来看,它们似乎非常具体,并且使每个人都需要一段时间。我是一名大学生,每当我在黑客马拉松中工作时,我都会花很长时间才能弄清楚如何执行API调用。我承认我没有经验,但是总的来说,弄清楚新的API是否需要大的学习曲线,即使对于完成10个左右的个人来说也是如此?
再次感谢您的宝贵时间:)
答案 0 :(得分:1)
我在您的终端中注意到您的MD5哈希是大写的。 MD5应该以小写形式输出。确保您转换成那个。
那是我的问题,我正在发送大写哈希值。
答案 1 :(得分:0)
如上所述,解决方案是哈希格式不正确。必须为十六进制字符串,问题已解决。
答案 2 :(得分:0)
访问Marvel API密钥时,我也遇到了类似的问题。对于仍在挣扎的用户,这是我的模板代码(在jupyter笔记本中使用)。
# import dependencies
import hashlib #this is needed for the hashing library
import time #this is needed to produce a time stamp
import json #Marvel provides its information in json format
import requests #This is used to request information from the API
#Constructing the Hash
m = hashlib.md5() #I'm assigning the method to the variable m. Marvel
#requires md5 hashing, but I could also use SHA256 or others for APIS other
#than Marvel's
ts = str(time.time()) #This creates the time stamp as a string
ts_byte = bytes(ts, 'utf-8') #This converts the timestamp into a byte
m.update(ts_byte) # I add the timestamp (in byte format) to the hash
m.update(b"my_private_key") #I add the private key to
#the hash.Notice I added the b in front of the string to convert it to byte
#format, which is required for md5
m.update(b"b2aeb1c91ad82792e4583eb08509f87a") #And now I add my public key to
#the hash
hasht = m.hexdigest() #Marvel requires the string to be in hex; they
#don't say this in their API documentation, unfortunately.
#constructing the query
base_url = "https://gateway.marvel.com" #provided in Marvel API documentation
api_key = "b2aeb1c91ad82792e4583eb08509f87a" #My public key
query = "/v1/public/events" +"?" #My query is for all the events in Marvel Uni
#Building the actual query from the information above
query_url = base_url + query +"ts=" + ts+ "&apikey=" + api_key + "&hash=" +
hasht
print(query_url) #I like to look at the query before I make the request to
#ensure that it's accurate.
#Making the API request and receiving info back as a json
data = requests.get(query_url).json()
print(data) #I like to view the data to make sure I received it correctly
给予应有的信誉,我非常依赖此博客。您可以在此处获取有关hashlib库的更多信息。 https://docs.python.org/3/library/hashlib.html