让 Tweepy 运行

让 Tweepy 运行

我无法开始tweepy工作。我做错了什么?:

nuc@nuc:~$ python
Python 2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Aug 21 2014, 18:22:21) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import tweepy
>>> user = tweepy.api.get_user('twitter')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nuc/anaconda/lib/python2.7/site-packages/tweepy/binder.py", line 239, in _call
    return method.execute()
  File "/home/nuc/anaconda/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
>>> 

答案1

如果没有注册您的凭证,您就无法使用 tweepy API。

Tweepy 尝试让 OAuth 尽可能地为你省去麻烦。要开始此过程,我们需要向 Twitter 注册我们的客户端应用程序。创建一个新应用程序,完成后,你应该拥有你的消费者令牌和密钥。请将这两个信息放在手边,你会需要它们。

看这个例子代码:

from __future__ import absolute_import, print_function

import tweepy

# == OAuth Authentication ==
#
# This mode of authentication is the new preferred way
# of authenticating with Twitter.

# The consumer keys can be found on your application's Details
# page located at https://dev.twitter.com/apps (under "OAuth settings")
consumer_key=""
consumer_secret=""

# The access tokens can be found on your applications's Details
# page located at https://dev.twitter.com/apps (located
# under "Your access token")
access_token=""
access_token_secret=""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.secure = True
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

# If the authentication was successful, you should
# see the name of the account print out
print(api.me().name)

来源:http://docs.tweepy.org/en/v3.2.0/auth_tutorial.html

相关内容