Search code examples
pythonpython-3.ximage-uploadingpython-3.5imgur

Not able to upload image in created album: Imgur python


I am trying to create an album and upload images into it using imgurpython. However, I am not able to attach images to the album. It is giving me following error: (403) The album you're requesting does not belong to your account. Following is my code:

client = ImgurClient(client_id, client_secret)
def CreateAlbumAndUploadImages(albumName,albumDescription,images):
    fields = {}
    fields['title'] = albumName
    fields['description'] = albumDescription
    fields['privacy'] = 'public'
    x = client.create_album(fields)
    print(x)
    y = client.album_add_images(x['id'],images)  #error here
    print(y)
    return x
def UploadPhoto(images):
    config = {
        'name':  'some image name here',
        'title': 'some title here',
        'description': 'some description here'}    
    image = client.upload_from_path(image_path, config=config, anon=False)
    print("Done")
    print(image)
    return image

def main():
    #x = CreateAlbum('Album 101','Album 101 desciption')
    id = []
    id.append( UploadPhoto(['image1.jpg'])['id'])
    id.append( UploadPhoto(['image2.jpg'])['id'])
    x = CreateAlbumAndUploadImages('albumNameHere','some description here',id)
    pass

if __name__ == '__main__':
    main()

NOTE: I am trying to build a bot, hence calling authorize on web is not an option


Solution

  • I had a similar problem and figured it out by realizing that I hadn't fully authenticated the client. To do so you must do this:

    client = ImgurClient(client_id,client_secret, refresh_token)
    client.set_user_auth(access_token, refresh_token)
    

    Then it should work. If you need to get access and refresh token then do:

    client = ImgurClient(client_id, client_secret)
    print client.get_auth_url('pin') #go to page and copy down pin
    creds = client.authorize(raw_input('Pin: '), 'pin')
    client.set_user_auth(creds['access_token'], creds['refresh_token'])
    #You will only need to do this once per user, store tokens
    

    After that, just save your access token and refresh token and just include it in the first example.