【问题标题】:Twitter API: simple status update (Python)Twitter API:简单状态更新(Python)
【发布时间】:2011-05-27 06:39:00
【问题描述】:

我一直在寻找一种从 Python 客户端更新我的 Twitter 状态的方法。由于该客户端只需要访问一个 Twitter 帐户,因此应该可以使用预先生成的 oauth_token 和机密来执行此操作,根据http://dev.twitter.com/pages/oauth_single_token

但是示例代码似乎不起作用,我得到“无法验证您”或“签名不正确”..

由于那里有很多不同的 python-twitter 库(并且并非所有这些库都是最新的),如果有人能指出一个当前正在处理 POST 请求或发布的库,我将不胜感激一些示例代码!

更新: 我已经尝试过 Pavel 的解决方案,只要新消息只有一个字长,它就可以工作,但是一旦它包含空格,我就会收到此错误:

status = api.PostUpdate('hello world')
Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python26\lib\site-packages\python_twitter\twitter.py", line 2459, in PostUpdate
        self._CheckForTwitterError(data)
      File "C:\Python26\lib\site-packages\python_twitter\twitter.py", line 3394, in _CheckForTwitterErro
    r
        raise TwitterError(data['error'])
    python_twitter.twitter.TwitterError: Incorrect signature

如果更新只是一个词,它可以工作:

status = api.PostUpdate('helloworld')
{'status': 'helloworld'}

知道为什么会发生这种情况吗?

非常感谢,

霍夫

【问题讨论】:

  • 抱歉,我无法重现您的错误。如果正确,我会仔细检查 OAuth 过程。

标签: python oauth twitter twitter-oauth


【解决方案1】:

我已经编写了一些与此问题相关的内容。

import tweepy
consumer_key = Your_consumer_key
consumer_secret = Your_consumer_secret
access_token = Your_access_token
access_token_secret = Your_access_token_secret_key

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
single_tweet = 'hello world'
api.update_status(single_tweet)
print "successfully Updated"

【讨论】:

    【解决方案2】:

    Python-Twitter 文档的最新位置现在位于 GitHub 上(谷歌代码页指向您。)

    您现在不再需要使用 python-twitter 附带的命令行工具来获取全套访问令牌和机密,https://dev.twitter.com 将让您在注册应用时请求它们。

    一旦你有了四个不同的凭证值,你要做的第一件事就是通过 API 测试来测试它们:

    api = twitter.Api(consumer_key='consumer_key', consumer_secret='consumer_secret', access_token_key='access_token', access_token_secret='access_token_secret')

    打印 api.VerifyCredentials()

    这将显示您的凭据是否有效。如果您收到错误,下一步是将 debugHTTP=True 传递给 Api() 调用 - 这将导致打印所有 HTTP 对话,以便您可以看到 Twitter 错误消息。

    一旦凭据正常工作,您就可以尝试调用 PostUpdate() 甚至只是 GetTimeline()

    【讨论】:

      【解决方案3】:

      我已经能够使用另一个库解决这个问题 - 所以我将在此处发布我的解决方案以供参考:

      import tweepy
      # http://dev.twitter.com/apps/myappid
      CONSUMER_KEY = 'my consumer key'
      CONSUMER_SECRET = 'my consumer secret'
      # http://dev.twitter.com/apps/myappid/my_token
      ACCESS_TOKEN_KEY= 'my access token key'
      ACCESS_TOKEN_SECRET= 'my access token secret'
      
      def tweet(status):
          '''
          updates the status of my twitter account
          requires tweepy (https://github.com/joshthecoder/tweepy)
          '''
          if len(status) > 140:
              raise Exception('status message is too long!')
          auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
          auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
          api = tweepy.API(auth)
          result = api.update_status(status)
          return result
      

      【讨论】:

        【解决方案4】:

        你可能对此http://code.google.com/p/python-twitter/感兴趣

        不幸的是,为了公平起见,文档不存在,上一次“发布”是在 2009 年。

        我使用了 hg 的代码:

        wget http://python-twitter.googlecode.com/hg/get_access_token.py
        wget http://python-twitter.googlecode.com/hg/twitter.py
        

        在(长时间)应用注册过程 (http://dev.twitter.com/pages/auth#register) 之后,您应该拥有消费者密钥和秘密。它们对于应用来说是独一无二的。

        接下来您需要将应用程序与您的帐户相关联,根据源代码中的说明(原文如此!)编辑 get_access_token.py 并运行。您现在应该拥有 Twitter 访问令牌密钥和密钥。

        >>> import twitter
        >>> api = twitter.Api(consumer_key='consumer_key',
                    consumer_secret='consumer_secret', access_token_key='access_token',
                    access_token_secret='access_token_secret')
        >>> status = api.PostUpdate('I love python-twitter!')
        >>> print status.text
        I love python-twitter!
        

        它适用于我http://twitter.com/#!/pawelprazak/status/16504039403425792(不确定它是否对所有人可见)

        也就是说我必须补充说我不喜欢代码,所以如果我要使用它,我会重写它。

        编辑:我已经把这个例子讲得更清楚了。

        【讨论】:

        • +1 用于 python-twitter,很棒且易于使用的库。不过,Oauth 可能会带您阅读以了解确切的过程;这肯定花了我一段时间。
        • 感谢 Pawel 的回答,我已经尝试过了,有趣的是,当我的状态只有一个字长时它可以工作,但只要其中有空格,我就会收到此错误:跨度>
        • 我已经检查了答案并进行了相应的更新。它有效。
        猜你喜欢
        • 2016-03-12
        • 2012-01-11
        • 1970-01-01
        • 1970-01-01
        • 2012-03-15
        • 1970-01-01
        • 2011-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多