Marvel API调用中的哈希值、时间戳和密钥组合无效。

8
我正在尝试创建一个Marvel API调用。
这里有一个关于授权的链接: https://developer.marvel.com/documentation/authorization 我正在尝试创建一个服务器端应用程序,因此根据上面的链接,我需要一个时间戳、apikey和hash url参数。Hash需要是以下表单的md5哈希: md5(timestamp + privateKey + publicKey),而apikey url参数是我的公钥。
这是我的代码,我正在使用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());

以下是输出结果:

$python3 request.py
https://gateway.marvel.com:443/v1/public/characters...${URL TRUNCATED FOR READABILITY)
{'code': 'InvalidCredentials', 'message': 'That hash, timestamp, and key combination is invalid'}
$

我不确定是什么导致组合无效。

如果需要,我可以提供更多信息。任何信息都将不胜感激。谢谢!

编辑:

我对API调用还有点陌生。有没有了解如何执行它们的更多资源?到目前为止,根据我的有限经验,它们似乎非常具体,让每个人都需要花费一些时间才能使其正常工作。我是一名大学生,在黑客马拉松中工作时,我需要花费很长时间才能弄清楚如何执行API调用。我承认我没有经验,但总的来说,即使对于已经完成了10个左右的人来说,弄清新的API是否需要一个大的学习曲线呢?

再次感谢您的时间 :)


1
我尝试了一下你的代码,我认为m_hash.hexdigest()可能是解决方案。我不得不硬编码ts = 1apikey = 1234,以确保从漫威的示例中得到正确的哈希值。另外,ts_str = str(int(ts));使用int而不是float? - TrebledJ
嗨,你说得对,我也刚刚弄明白了,是哈希的格式问题:(感谢你的时间,我已经点赞了你的回答,因为它是正确的。现在time.time()返回一个十进制值 - 我多次打印了ts的值,看起来它会根据秒数变化 - 所以只要我不在同一秒内进行多个API调用,整数值就完全没问题。如果我这样做了,我可能需要使用浮点值。谢谢 :) - Philosopher-Programmer
4个回答

3

当访问漫威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


2
我注意到您的终端中MD5哈希值是大写的。MD5应该输出为小写。请确保进行转换。
那就是我的问题了,我发送的是一个大写哈希值。
最初的回答:MD5哈希值应该是小写的。

0
如上所述,问题在于哈希值的格式不正确。需要使用十六进制字符串来解决该问题。

1
请勿参考注释。将更正或解决方案附在您的答案中。 - TrebledJ

0

你的最终URL应该像这样:

http://gateway.marvel.com/v1/public/characters?apikey=(public_key)&ts=1&hash=(md5_type_hash)

所以,你已经在开发者账户中拥有公钥。但是,你如何生成md5_type_hash呢?

ts=1只使用了1。因此,你的模式应该是这样的:

1 + private_key(ee7) + public_key(aa3)。例如:将1ee7aa3转换为MD5-Hash = 1ca3591360a252817c30a16b615b0afa (md5_type_hash)

你可以从这个网站创建:https://www.md5hashgenerator.com

完成了,现在你可以使用Marvel API了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接