Bartłomiej 'furas' Burek
furas.pl
# prywatne notatki - Python, Linux, Machine Learning, etc.

Requests: use Imgur API to upload image[GB]

First you have to create normal account on Imgur.

After loging to normal account you can go to https://api.imgur.com/oauth2/addclient to register application.

It needs application name and email. Type of authorization depends on how you will use it.

python requests - imgur API - 1

You should get API keys

python requests - imgur API - 2

Which you can use with API

To get information:

import requests

headers = {
    'Authorization': 'Client-ID f1XXXXXXXXXXXXX',
}

#image: https://i.imgur.com/cvWgXFc.jpg
imageHash = 'cvWgXFc'

r = requests.get(f'https://api.imgur.com/3/image/{imageHash}', headers=headers)
print('status:', r.status_code)
data = r.json()

print(data)
print('size:', data['data']['size'])

Result:

status: 200

{'data': {'id': 'cvWgXFc', 'title': None, 'description': None, 'datetime': 1579572289, 'type': 'image/jpeg', 'animated': False, 'width': 506, 'height': 500, 'size': 89341, 'views': 8087, 'bandwidth': 722500667, 'vote': None, 'favorite': False, 'nsfw': False, 'section': None, 'account_url': None, 'account_id': None, 'is_ad': False, 'in_most_viral': False, 'has_sound': False, 'tags': [], 'ad_type': 0, 'ad_url': '', 'edited': '0', 'in_gallery': False, 'link': 'https://i.imgur.com/cvWgXFc.jpg', 'ad_config': {'safeFlags': ['onsfw_mod_safe', 'share', 'page_load'], 'highRiskFlags': [], 'unsafeFlags': ['not_in_gallery', 'sixth_mod_unsafe'], 'wallUnsafeFlags': [], 'showsAds': False}}, 'success': True, 'status': 200}

size: 89341

To upload:

import requests
import base64

headers = {
    'Authorization': 'Client-ID f1XXXXXXXXXXXXX',
}

params = {
  'image': base64.b64encode(open('images.png', 'rb').read())
}

r = requests.post(f'https://api.imgur.com/3/image', headers=headers, data=params)
print('status:', r.status_code)
data = r.json()
print(data)

BTW: you can see your registered applications and regenerate API key (if you forget it) on https://imgur.com/account/settings/apps

python requests - imgur API - 3
Książki: python-dla-kazdego-podstawy-programowania python-wprowadzenie python-leksykon-kieszonkowy python-receptury python-programuj-szybko-i-wydajnie python-projekty-do-wykorzystania black-hat-python-jezyk-python-dla-hackerow-i-pentesterow efektywny-python-59-sposobow-na-lepszy-kod tdd-w-praktyce-niezawodny-kod-w-jezyku-python aplikacje-internetowe-z-django-najlepsze-receptury