Arturo
Arturo

Reputation: 3779

Submitting uploaded Imgur image to gallery

This is a follow up question to: uploading a file to imgur via python

I just uploaded an image to Imgur, but I've noticed it isn't public for view. At their API website they say that after its uploaded if you want it to be public you should submit it to the gallery.

I coded a small app that could do this with one image, but I'm not sure if it's not working because I am not making the request right, or because I might be missing using the Oauth2 module, that so far I haven't required using it (I'm just uploading images but I don't have a need to see what other users are uploading with my app).

import base64
import json
import requests

from base64 import b64encode

client_id = '6a06c6bb8360e78'

headers = {"Authorization": "Client-ID 6a06c6bb8360e78"}

api_key = '6a06c6bb8360e78'

url = "https://api.imgur.com/3/upload.json"

j1 = requests.post(
    url, 
    headers = headers,
    data = {
        'key': api_key, 
        'image': b64encode(open('1.jpg', 'rb').read()),
        'type': 'base64',
        'name': '1.jpg',
        'title': 'Picture no. 1'
    }
)


data=json.loads(j1.text)['data']
data_id=data['id']

data_id_str=data_id.encode('ascii','ignore')

# Now I try to push the iamge to the gallery with id data:

url_gallery = "https://api.imgur.com/3/gallery/image/{id}.json"

r2 = requests.post(url_gallery, headers=headers, data={'title': 'First image', 'terms': '1', 'id': data_id_str})

The answer I get after doing: r2.text is a 403 error:

u'{"data":{"error":"Unauthorized",
"request":"\\/3\\/gallery\\/image\\/{id}.json",
"parameters":"terms = 1, id = 1jhIc2R, title = First
image","method":"POST"},"success":false,"status":403}'

This is the Imgur API link for submitting images, I'm not sure where I did wrong: https://api.imgur.com/endpoints/gallery#to-gallery

Upvotes: 0

Views: 2835

Answers (1)

user178572
user178572

Reputation: 31

Alan from Imgur here. You need to be authenticated to submit to the gallery (hence the Unauthorized error). Currently it looks like you're uploading anonymously, and then also trying to submit to the gallery anonymously. You'll need to get an access_token from OAuth2 for the gallery part.

Upvotes: 3

Related Questions