亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 PyImgur 的記錄方式上傳,上傳拒絕顯示在圖庫中

使用 PyImgur 的記錄方式上傳,上傳拒絕顯示在圖庫中

慕的地8271018 2023-07-18 14:56:23
我使用 PyImgur 文檔中的代碼:import pyimgurCLIENT_ID = "xxx"PATH = "image.jpg"im = pyimgur.Imgur(CLIENT_ID)uploaded_image = im.upload_image(PATH, title="Uploaded with PyImgur")print(uploaded_image.title)print(uploaded_image.link)print(uploaded_image.size)print(uploaded_image.type)但它沒有出現在我的畫廊中
查看完整描述

1 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

問題是所有 API 是如何工作的(不僅僅是imgur API)。


CLIENT_ID允許訪問 API,但只能作為匿名用戶。


具有相同內容的程序CLIENT_ID可以由不同的用戶(不僅僅是您)使用,并且每個用戶都必須允許訪問他/她的數據。


第一個程序必須生成特殊的 URL imgur.com(我不確定是否需要CLIENT_SECRET這個)


 im = pyimgur.Imgur(CLIENT_ID)#, client_secret=CLIENT_SECRET)


 auth_url = im.authorization_url('pin')

接下來,您必須在網絡瀏覽器中打開此 URL,它會要求您登錄,imgur.com并且可能會詢問您是否允許該程序訪問您的數據。如果您允許,那么它將顯示帶有唯一編號的頁面pin,您必須在程序中使用該編號


 access_token, refresh_token = im.exchange_pin(pin)

然后程序將可以訪問您的數據并將其上傳到您的帳戶 - 直到您關閉程序。


這還為您提供了access_token(就像一個數字中的登錄名和密碼)在重新啟動程序后可以使用哪個程序來訪問您的數據 - 因此您不必再次登錄并imgur.com復制pin.


出于安全原因,access_token它會在 60 分鐘后過期,因此您還可以獲得refresh_token(永不過期)哪個程序必須每 60 分鐘使用一次來生成新的access_token.


 im = pyimgur.Imgur(CLIENT_ID, access_token=access_token, refresh_token=refresh_token)

現在,它會在重新啟動程序后將圖像上傳到您的帳戶。


最少的工作代碼。


我使用從文件try/except加載或打開網頁來生成. 我在 Google API 的一些示例中看到了這種方法(但它用于將令牌保存在文件中)。access_tokenrefresh_tokenpinaccess_tokenrefresh_tokenpickle


import pyimgur

import webbrowser


CLIENT_ID = "123456789012345"

#CLIENT_SECRET = 'xxxyyyzzz'


# access to user data


try:

    # try to loading access_token, refresh_token from file

    with open('tokens.txt') as f:

        access_token, refresh_token = f.read().strip().split()

        

    # run with access_token, refresh_token    

    im = pyimgur.Imgur(CLIENT_ID, access_token=access_token, refresh_token=refresh_token)

    

except FileNotFoundError as ex:

    # ask for new access_token, refresh_token when file doesn't exists

    

    # run without access_token, refresh_token

    im = pyimgur.Imgur(CLIENT_ID)  #, client_secret=CLIENT_SECRET)


    # generate url

    auth_url = im.authorization_url('pin')

    

    # open url in web browser

    webbrowser.open(auth_url)

    

    # ask for `pin` from web page

    pin = input("What is the pin? ")

    

    # get access_token, refresh_token

    access_token, refresh_token = im.exchange_pin(pin)


    # save access_token, refresh_token in file

    with open('tokens.txt', 'w') as f:

        f.write(f'{access_token} {refresh_token}')


# upload image


PATH = "lenna.png"


uploaded_image = im.upload_image(PATH, title="Uploaded with PyImgur")


print('[upload] title:', uploaded_image.title)

print('[upload] link :', uploaded_image.link)

print('[upload] size :', uploaded_image.size)

print('[upload] type :', uploaded_image.type)


# share with community - to see image (as post) on imgur's main page 

uploaded_image.submit_to_gallery(title='Shared with PyImgur')

這會將圖像添加到用戶帳戶,但我無法添加到選定的相冊。有upload_image(..., album=...),但需要專輯 ID,而不是名稱。使用im.search_gallery(query)我什至可以獲得相冊的 ID,但我總是收到錯誤,無法更改圖庫中相冊中的信息。


編輯:


此代碼將圖像添加到您的帳戶,您可以獲取其鏈接并將此鏈接通過郵件、消息發送給其他人或將鏈接放在某個論壇上,或使用它在您自己的網頁上顯示圖像


...但這不會將其發送到 Imgur 主頁上 - 它不會"share to community"。


你必須添加


uploaded_image.submit_to_gallery(title='Title of your post')

然后人們可以在Imgur 的主頁上看到它并投票。


我將這一行添加到上面的代碼中。


PyImgur源代碼:Image.submit_to_gallery()、Album.submit_to_gallery()


當您允許程序訪問您的數據時,您應該會看到您的程序Apps Used,并且您可以使用revoke access禁止程序訪問您的數據


我用于Client ID我的應用程序furas-imgur,因此在允許訪問我的數據后,我furas-imgur看到Apps Used

http://img1.sycdn.imooc.com//64b637c600011e6b10580379.jpg

查看完整回答
反對 回復 2023-07-18
  • 1 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號