不明白Tweepy错误

时间:2015-10-14 02:04:36

标签: python api tweepy

我只是掌握了python,我想开始使用API​​等等。我安装了Tweepy,拿到了我的Twitter密钥,并按照他们网站上的设置过程进行操作。它很好,直到,好吧,这里

import tweepy

auth = tweepy.OAuthHandler('qwertyuiop', 'asdfghjkl')
auth.set_access_token('qazsecftgbhujmkolp', 'plokmjuhbgtfcdeszaq')

api = tweepy.API(auth)

user = tweepy.api.get_user('twitter')

print user.screen_name

(当然还有我的真实钥匙)

返回错误

Traceback (most recent call last):
  File "tweeter.py", line 8, in <module>
    user = tweepy.api.get_user('twitter')
  File "/home/jeremiah/.local/lib/python2.7/site-packages/tweepy/binder.py", line 243, in _call
    return method.execute()
  File "/home/jeremiah/.local/lib/python2.7/site-packages/tweepy/binder.py", line 189, in execute
    raise TweepError('Failed to send request: %s' % e)
tweepy.error.TweepError: Failed to send request: local variable 'auth' referenced before assignment

我不知道。定义auth是我做的第一件事。此外,这是在网站的介绍课程之后。

user = tweepy.api.get_user('twitter')之前似乎一切正常。如果我把它以及它下面的所有内容拿出来,那很好,或者如果我用

替换它
public_tweets = api.home_timeline()
for tweet in public_tweets:
    print tweet.text

没关系。

那是什么? Tweepy搞砸了,我的电脑是不是很复杂,或者我只是错过了一些明显的东西?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您应该能够使用tweepy.API classget_user方法(您已将其分配给变量名称api)。

尝试:

user = api.get_user(id='__your__username__')  # returns the user specified by id

或者:

user = api.me()  # returns YOUR authenticated user object

您需要在括号中指定用户名,或使用me()方法(而不是文字'twitter'(从@twitter返回用户对象)的原因,因为您&# 39; ve要求那个用户!)是因为get_user method

  

返回有关指定用户的信息。

     

<强>参数:
    id - 指定用户的ID或屏幕名称。

     

user_id - 指定用户的ID。当有效用户ID也是有效的屏幕名称时,有助于消除歧义。

     

screen_name - 指定用户的屏幕名称。当有效的屏幕名称也是用户ID时,有助于消除歧义。

关于你的代码,请注意它之间的区别,这会引发异常:

user = tweepy.api.get_user('twitter'). 

而且,这不会引发异常

public_tweets = api.home_timeline()   '## NOT tweepy.api.home_timeline()

在后者(没有错误)中,您正在使用api变量来调用home_timeline方法。