Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid hash, timestamp, and key combination in Marvel API Call

I'm trying to form a Marvel API Call.

Here's a link on authorization: https://developer.marvel.com/documentation/authorization

I'm attempting to create a server-side application, so according to the link above, I need a timestamp, apikey, and hash url parameters. The hash needs be a md5 hash of the form: md5(timestamp + privateKey + publicKey) and the apikey url param is my public key.

Here's my code, I'm making the request in Python 3, using the request library to form the request, the time library to form the timestamp, and the hashlib library to form the hash.

#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());

Here's the output:

$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'}
$

I'm not sure what exactly is causing the combination to be invalid.

I can provide more info on request. Any info would be appreciated. Thank you!

EDIT:

I'm a little new to API calls in general. Are there any resources for understanding more about how to perform them? So far with my limited experience they seem very specific, and getting each one to work takes a while. I'm a college student and whenever I work in hackathons it takes me a long time just to figure out how to perform the API call. I admit I'm not experienced, but in general does figuring out new API's require a large learning curve, even for individuals who have done 10 or so of them?

Again, thanks for your time :)

like image 656
Philosopher-Programmer Avatar asked Nov 17 '18 23:11

Philosopher-Programmer


2 Answers

I've also had similar issues when accessing the Marvel API key. For those that are still struggling, here is my templated code (that I use in a jupyter notebook).

# 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

Give credit where credit is due, I relied on this blog a lot. You can go here for more information on the hashlib library. https://docs.python.org/3/library/hashlib.html

like image 57
Heather Claxton Avatar answered Nov 11 '22 14:11

Heather Claxton


I noticed in your terminal your MD5 hash is uppercase. MD5 should output in lowercase. Make sure you convert to that.

That was my issue, I was sending an uppercase hash.

like image 20
Advait Junnarkar Avatar answered Nov 11 '22 13:11

Advait Junnarkar