使用PyImgur记录的上载方式,上载拒绝显示在图库中

问题描述

我使用Pyimgur的文档中的以下代码

<div class="white-rounded-Box flex-column mx-auto my-auto p-sm-5">
 <h2 class="d-flex align-self-center"
     style="color:var(--my-blue)">What would you like to do?</h2>

 <button class="btn btn-primary d-flex align-self-end mb-sm-1"
      type="button">
  Create a new request
</button>
<button class="btn btn-primary d-flex align-self-end mt-sm-1"
      type="button">
Edit an old request
</button>
<MyBox class="collpase" />
</div>

但是它没有显示在我的画廊中

解决方法

问题是所有API的工作方式(不仅是imgur API)。

CLIENT_ID允许访问API,但只能以匿名用户身份访问。

具有相同CLIENT_ID的程序可以由不同的用户使用(不仅由您使用),而且每个用户都必须允许访问他/她的数据。

第一个程序必须生成指向imgur.com的特殊URL(我不确定是否为此需要CLIENT_SECRET

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

 auth_url = im.authorization_url('pin')

接下来,您必须在Web浏览器中打开此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从文件中加载access_tokenrefresh_token或为pin打开网页来生成access_tokenrefresh_token。我在Google API的一些示例中看到了这种方法(但它使用pickle将令牌保留在文件中)。

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,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,但总是会收到无法更改图库中相册信息的错误。


编辑:

此代码将图像添加到您的帐户中,您可以获取其链接并将其发送给其他人,以邮件,messanger或在某个论坛上放置链接,或使用它在您自己的网页上显示图像

...但是这不会在Imgur主页上发送-它不是"share to community"

您必须添加

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

然后人们可以在Imgur's main page上看到它并对其进行投票。

我将此行添加到了上面的代码中。

PyImgur源代码:Image.submit_to_gallery()Album.submit_to_gallery()


当允许程序访问数据时,您应该在Apps Used中看到程序,并且可以使用revoke access禁止程序访问数据

我在应用程序Client ID中使用furas-imgur,因此在允许访问数据后,我在furas-imgur中看到了Apps Used

enter image description here