【问题标题】:How to post image to twitter with Twython?如何使用 Twython 将图像发布到 Twitter?
【发布时间】:2015-01-21 13:00:10
【问题描述】:

上个月我的这个小脚本运行良好

from twython import Twython
import glob
import random

app_key = "XXX"
app_secret = "XXX"
oauth_token = "XXX"
oauth_token_secret = "XXX"
twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)

    def RandomImageTwitt(folder):
        #Takes the folder where your images are as the input
        images = glob.glob(folder + "*")
        image_open = open(images[random.randint(0,len(images))-1])
        twitter.update_status_with_media(media=image_open)

RandomImageTwitt("/home/XXX/.reddit-twitter-image/XXX/")

但现在 Twitter 已经弃用了这种方法。 Twython 告诉我应该使用 Twython.upload_media,但我找不到任何关于它使用的文档。甚至 Twython 官方网站仍然列出了一个带有 update_status_with_media 的示例。

有人知道怎么做或在哪里可以找到一些示例/信息吗?

【问题讨论】:

    标签: python twitter twython


    【解决方案1】:

    当您执行 twitter.update_status 时,是必需的 status 和 media_ids

    twitter.update_status(status='hello this is a status', media_ids=image_ids['media_id'])
    

    【讨论】:

    • 添加 status= 和 media_ids= 对我有用,谢谢!
    【解决方案2】:

    好的,我遇到了同样的问题,我搞砸了它并让它工作了。

    我已经把它放到你下面的代码中(虽然没有测试过)

    from twython import Twython
    import glob
    import random
    
    app_key = "XXX"
    app_secret = "XXX"
    oauth_token = "XXX"
    oauth_token_secret = "XXX"
    twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)
    
        def RandomImageTwitt(folder):
            #Takes the folder where your images are as the input
            images = glob.glob(folder + "*")
            image_open = open(images[random.randint(0,len(images))-1])
            #new code starts here 
            image_ids = twitter.upload_media(media=image_open)
            twitter.update_status('hello this is a status',image_ids['media_id'])
    
    
    RandomImageTwitt("/home/XXX/.reddit-twitter-image/XXX/")
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-12
    • 2015-04-24
    • 2017-06-13
    • 2013-01-15
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多