Viktor Viktor -4 years ago 46
Python Question

Connecting to API, getting 409 back

I am completely new to APIs and I'm having a hard time connecting to Marvel API. Documentation says my connection link should be something like this:


https://gateway.marvel.com:443/v1/public/characters?nameStartsWith=Spider&apikey=XXXXX&hash=XXXXXX


hash is a md5 string containg from time stamp (or other string that would be changing every request, private key and public key)

I registered on their website and I have my keys, this is how I am trying to make a call to find Character who's name begins with "Spider"

Creating every request changing string:

t ='{:%H:%M:%S}'.format(datetime.datetime.now())
t = t.replace(":", "")


output: "213944"

hashing:

m = hashlib.md5(t+private+public).hexdigest()


pack I am trying to use:

pack = 'https://gateway.marvel.com:443/v1/public/characters?nameStartsWith=Spider&apikey=<ACTUAL-API-KEY>&hash=' + m


Requesting printing response:

response = requests.get(pack)
print(response)


I keep getting back
Response[409]
and according to API documentation it means that I have wrong timestamp, public or private key. I spent hours already trying to find whats wrong and I need some help.

This is a direct link to API documentation

Answer Source

As per the documentation, you are missing the ts parameter. Add your t variable as the ts parameter.

pack = 'https://gateway.marvel.com:443/v1/public/characters?nameStartsWith=Spider&apikey=<ACTUAL-API-KEY>&hash=' + m + '&ts=' +t

Also, the documentation states that ts to change on a request-by-request basis. Consider using t=str(int(time.time()*1000)) instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download