This is the API documentation for imgur that I reffered to for uploading requests: http://api.imgur.com...ces_anon#upload
This is the code I've been working on (Also the code that doesn't work)
#apikey is 02b62fd8f1d5e78321e62bb42ced459e
#url for uploading is http://api.imgur.com/2/upload
#image file in the directory is pic.png
import urllib
import urllib2
picture = open('pic.png')
url = 'http://api.imgur.com/2/upload'
parameters = {'key' : '02b62fd8f1d5e78321e62bb42ced459e', 'image' : picture}
data = urllib.urlencode(parameters)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
I tried figuring out what the problem may be. Reading the documentation on imgur I found that the image parameter must be "A binary file, base64 data, or a URL." I tried using a url for the parameter and it worked perfectly.
i.e.
This code works but I need to be able to use an actual image file on my computer instead of a url to upload an image
#apikey is 02b62fd8f1d5e78321e62bb42ced459e
#url for uploading is http://api.imgur.com/2/upload
#image file in the directory is pic.png
import urllib
import urllib2
picture = 'http://www.opticianonline.net/blogs/big-optometry-blog/eiffel-tower-picture.jpg'
url = 'http://api.imgur.com/2/upload'
parameters = {'key' : '02b62fd8f1d5e78321e62bb42ced459e', 'image' : picture}
data = urllib.urlencode(parameters)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
So I figured that my problem is with the image parameter. I can't seem to pass an open file in python as the parameter. What can I do?

New Topic/Question
Reply




MultiQuote




|