Question

I am trying to upload images to imgur, and I found the way to do it below:

img = requests.post(
    'https://api.imgur.com/3/upload.json',
    headers = {'Authorization': 'Client-ID <my client-id>'},
    data = {
        'key': '<my secret key>',
        'title': 'test'
        'image': <path to image>
    }
)
j = json.loads(img.text)
print j

Imgur API for image upload: https://api.imgur.com/endpoints/image#image-upload

This works fine, but I want to be able to upload the images as a user. Where do I put my username and password?

Était-ce utile?

La solution

You'll need to authenticate your requests to imgur via oauth2. The account that is used to authenticate will be the account that does the uploads.

A full description of oauth is a bit beyond the scope of this answer, but in short, you're using the right http library in requests because it is often simpler than other approaches.

I like requests-oauthlib library which is recommended by requests. It works well with twitter's api too.

I should also note that imgur has a sample python app called imgur-python. Check out main.py for a bit more of a lead on things.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top