Search code examples
pythonpython-requestsimgur

How to create an account, and upload an image with that account with imgur


I am designing a python GUI, one of its functions is taking a screenshot, uploading it to Imgur and then getting the URL. Though I am having issues understanding the documentation (especially since it says you need to create an account through the API, but not how to do it.). Would anyone be able to explain how exactly to create an account and then upload an image using it?

Note: I am using PIL to get the screenshots, I would prefer you explain it as code written with the requests library or maybe curl (as that isn't too hard to move to python with requests), and I'll be saving only the refresh token in the program, as it would be hardcoded (But the user can change it) and I don't want the user to authenticate.

Thanks in advance.

Edit 1: Also, I will not use imgurpython as it is outdated.


Solution

  • 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.

    enter image description here

    You should get API keys

    enter image description here

    Which you can use with API

    To get information:

    import requests
    
    headers = {
        'Authorization': 'Client-ID f1XXXXXXXXXXXXX',
    }
    
    #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 keys (if you forget it) after login on https://imgur.com/account/settings/apps

    enter image description here