在创建的相册中无法上传图片:Imgur Python

5

我正在尝试使用imgurpython创建一个相册并上传图片。但是,我无法将图片附加到该相册中。它给了我以下错误:(403) The album you're requesting does not belong to your account。以下是我的代码:

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()

注意: 我正在尝试构建一个机器人,因此在网络上调用授权不是一个选项


在添加了你所接受的答案中提到的 set_user_auth 后,你是否已经成功地使其工作?有什么遗漏的地方吗?我也遇到了类似的情况,想知道解决方法。 - Govind Kailas
1个回答

2

我曾经遇到过类似的问题,后来发现是因为客户端没有完全通过身份验证。要解决这个问题,您需要执行以下操作:

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

那么它应该可以工作了。如果您需要获取访问和刷新令牌,请执行以下操作:

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

之后,只需保存您的访问令牌和刷新令牌,并将其包含在第一个示例中即可。


1
我已经尝试使用相册ID这种方法,但它抛出了一个错误 imgurpython.helpers.error.ImgurClientError: (400) You are not the owner of album<ALBUM ID> - Govind Kailas
@GovindKailas 你解决了吗? - Aditya Rajgor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接