【问题标题】:Not able to upload image in created album: Imgur python无法在创建的相册中上传图像:Imgur python
【发布时间】:2016-03-19 06:07:55
【问题描述】:

我正在尝试使用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 后,您是否能够正常工作?缺少什么,我陷入了类似的情况,想知道解决方法。

标签: python python-3.x image-uploading python-3.5 imgur


【解决方案1】:

我遇到了类似的问题,并通过意识到我没有完全验证客户端来解决这个问题。为此,您必须这样做:

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

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

【讨论】:

  • 我用专辑ID尝试过这种方法,它抛出了一个错误imgurpython.helpers.error.ImgurClientError: (400) You are not the owner of album<ALBUM ID>
最近更新 更多