How to generate an anonymous request to Imgur APIv3

Some time ago, I created a python function that took the image URL and passed it to Imgur API v2. Since I was informed that API v2 would be deprecated, I tried to do this using API v3.

As the Imgur API documentation says:

[Sending] the authorization header with your client_id along with your requests [...] also works if you want to anonymously upload images (without linking the image to your account). This allows us to find out which application is accessing the API. **

Authorization: YOURCLIENTID customer identifier

I donโ€™t understand (especially with italics) if they mean that the title should be {'Authorization': 'Client-ID ' + clientID} , or {'Authorization: Client-ID ': clientID} , or {'Authorization:', 'Client-ID ' + clientID} , or some other option ...

Anyway, I tried, and this is what I got (using Python 2.7.3):

 def sideLoad(imgURL): img = urllib.quote_plus(imgURL) req = urllib2.Request('https://api.imgur.com/3/image', urllib.urlencode([('image', img), ('key', clientSecret)])) req.add_header('Authorization', 'Client-ID ' + clientID) response = urllib2.urlopen(req) return response.geturl() 

It seems to me that he does everything Imgur wants me to do: I have the correct endpoint passing data to urllib2.Request makes a POST request according to the Python docs, I pass the image parameter using the URL encoded in form, I also tried to give him the secret of my client as a POST parameter, as I had an error: I need an identifier (even if there is no mention of the need to use my client secret somewhere in the relevant documentation). I am adding an authorization header and it seems to be the correct form, so ... why am I getting the 400: Bad error message?

Side question: I could debug it myself if I could see the actual Imgur error, but since it returns an HTTP error status, Python dies and gives me one of these disgusting stack traces. Is there a way Python could whine and give me a JSON error message that I know Imgur is returning?

+6
source share
2 answers

OK, I'll be damned. I tried to pull out the encoding functions and just line up the string, and I got it to work. I assume that the Imgur API expects a URL not associated with the form?

Oh ... or is it because I used both quote_plus () and url_encode (), having encoded the URL twice? It seems even more likely ...

This is my working solution, finally, for something that took me a day when I thought it would take more than an hour:

 def sideLoad(imgURL): img = urllib.quote_plus(imgURL) req = urllib2.Request('https://api.imgur.com/3/image', 'image=' + img) req.add_header('Authorization', 'Client-ID ' + clientID) response = urllib2.urlopen(req) response = json.loads(response.read()) return str(response[u'data'][u'link']) 

This is not the final version, note that you still do not have enough testing (I will see if I can get rid of quote_plus (), or maybe use url_encode more preferably), as well as error handling (especially for large gifs, the most common case of failure )

Hope this helps! I searched all over Google, Imgur and Stack Overflow, and the information about anonymous use of APIv3 was confusing (and drowned in a sea of โ€‹โ€‹completely terrifying OAuth2 things).

+5
source

In python 3.4 using urllib I was able to do it like this:

 import urllib.request import json opener = urllib.request.build_opener() opener.addheaders = [("Authorization", "Client-ID"+ yourClientId)] jsonStr = opener.open("https://api.imgur.com/3/image/"+pictureId).read().decode("utf-8") jsonObj = json.loads(jsonStr) #jsonObj is a python dictionary of the imgur json response. 
0
source

All Articles