Search code examples
pythonpython-3.ximgururllib3

Accessing Imgur API with Python 3.4.1 and Urllib3


I am trying to wrap my head around the Imgur API. I have found some good examples of how to send the authorization header to Imgur, however they all use urllib2, and I apparently, using pyhton 3.4.1 can only use urllib3.

So I have tried a couple of things and none of them seem to be working.

from this post I tried using the basic_auth header:

http = urllib3.PoolManager()
header = urllib3.make_headers(basic_auth="Client-ID" + CLIENT_ID)
r = http.request('GET', 'https://api.imgur.com/3/gallery/r/pics', headers=header)

that gives me a 403 error.

from this post I tried this method instead:

http = urllib3.PoolManager()
header= {"Content-Type": "text", "Authorization": "Client-ID" + CLIENT_ID}
r = http.request('GET', 'https://api.imgur.com/3/gallery/r/pics', headers=header)

that also returns a 403.

Now however I have got a step closer by reading the urllib3 documents and tried sending the Authorization as a field instead.

http = urllib3.PoolManager()
r = http.request('GET', 'https://api.imgur.com/3/gallery/r/pics', fields={"Authorization": "Client-ID " + CLIENT_ID})

this however returns a 401.

so can some one help me to figure out basic anonymous interaction with the Imgur API using these, or other methods?


Solution

  • Per imgur's API documentation, you have to send the auth header as such:

    Authorization: Client-ID YOUR_CLIENT_ID
    

    In this line:

    header = urllib3.make_headers(basic_auth="Client-ID" + CLIENT_ID)
    

    you are sending it as: Authorization: Client-IDYOUR_CLIENT_ID

    You need a space between.