2016-03-19 152 views
2

我正嘗試使用imgurpython創建相冊並將圖像上傳到其中。但是,我無法將圖像附加到相冊中。它給了我以下錯誤:(403) The album you're requesting does not belong to your account。以下是我的代碼:無法在創建的相冊中上傳圖像:Imgur python

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

注:我想建立一個機器人,因此呼籲授權的網站是不是一種選擇

回答

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 

之後,只需保存你的訪問令牌和刷新令牌,只是它包含在第一個例子。