【问题标题】:Tweepy and Twitter can't work togetherTweepy 和 Twitter 不能一起工作
【发布时间】:2014-03-11 09:37:18
【问题描述】:

编辑:我设法通过使用 virtualenv 使 Twitter 工作。不过,我还是想解决这个问题。

我成功安装并使用了 Tweepy。但是,当我尝试仅使用 Twitter API 时,我收到以下错误

AttributeError: 'module' object has no attribute 'oauth'

我使用的代码来自 Mining the Social Web, 2nd edition:

import twitter

def oauth_login():
    # XXX: Go to http://twitter.com/apps/new to create an app and get values
    # for these credentials that you'll need to provide in place of these
    # empty string values that are defined as placeholders.
    # See https://dev.twitter.com/docs/auth/oauth for more information 
    # on Twitter's OAuth implementation.

    CONSUMER_KEY = ''
    CONSUMER_SECRET = ''
    OAUTH_TOKEN = ''
    OAUTH_TOKEN_SECRET = ''

    auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
                               CONSUMER_KEY, CONSUMER_SECRET)

    twitter_api = twitter.Twitter(auth=auth)
    return twitter_api

并在以下调用后显示错误:

twitter_api = oauth_login() 
print twitter_api

我安装了 oauth,但不知道发生了什么。我需要安装 Tweepy,因为我已经编写了大量可与 Tweepy 一起使用的代码,并且我猜测问题是由于安装了 Tweepy 而引起的。

编辑:书中代码使用https://github.com/sixohsix/twitter

【问题讨论】:

  • 请添加使 AttributeError 出现的代码示例。你如何安装 Tweepy?
  • 加了代码(当然实际的代码有所有的键)
  • 您的问题是您(需要)安装了 python-twitter 和 Tweepy,并且只有安装了两者才会出现错误?只是为了确保我理解您的问题!
  • 我做了什么来尝试重现这个:pip install twitter tweepy(不在 virtualenv 中),然后将源代码写入文件,并添加行 oauth_login()(以运行该函数)。但是,它运行没有错误。你可以试试pip install --upgrade twitter tweepy 吗?也许你有一个旧版本...

标签: python twitter tweepy


【解决方案1】:

在最新版本的 Tweepy 中,OAuth 通过创建一个新的 OAuthHandler 实例来执行 - 没有 OAuth 类。此外,OAuthHandler 位于 auth 包中,而不是 oauth 包中。最后,所有四个身份验证密钥都不会传递给构造函数 - 访问令牌和访问令牌秘密是使用 set_access_token 方法设置的。

您的代码应如下所示:

auth = tweepy.auth.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2014-01-07
    • 2019-08-17
    • 2016-11-23
    • 2019-02-18
    • 2015-05-16
    • 2017-10-14
    相关资源
    最近更新 更多