经过多次尝试后无法访问Twitter

时间:2014-04-06 10:12:25

标签: python api twitter oauth twitter-oauth

我在Windows 7上工作,我试图使用tweepy甚至twitter1.14.2-python访问twitter。但我无法破解它。需要帮助。

TWEEPY

import tweepy

OAUTH_TOKEN = "defined here"
OAUTH_SECRET = "defined here"
CONSUMER_KEY = "defined here"
CONSUMER_SECRET = "defined here"

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

api = API.GetUserTimeline(screen_name="yyy")

错误:名称'API'未定义

TWITTER 1.14.2

import twitter
from twitter import *

tw = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET,CONSUMER_KEY, CONSUMER_SECRET))
tw.statuses.home_timeline()
tw.statuses.user_timeline(screen_name="yyy")

错误:没有名为OAuth的模块

我哪里出错?

2 个答案:

答案 0 :(得分:2)

由于tweepy.API类中没有定义/声明GetUserTimeline,所以我打算您尝试使用twitter.Api类的GetUserTimeline方法。

API通过twitter.Api类公开。

创建twitter.Api类的实例:

>>> import twitter
>>> api = twitter.Api()

使用登录凭据创建twitter.Api的实例。

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

要获取单个用户的公开状态消息,其中userTwitter "short name" or their user id

>>> statuses = api.GetUserTimeline(user)
>>> print [s.text for s in statuses]

答案 1 :(得分:1)

最后我通过tweepy找到出路

import tweepy

CONSUMER_KEY = 'Deliver here'
CONSUMER_SECRET = 'Deliver here'
OAUTH_TOKEN = 'Deliver here'
OAUTH_SECRET = 'Deliver here'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_SECRET)
urapi = tweepy.API(auth)
me = urapi.me()
print me().name
print me().screen_name
print me.followers_count
for status in tweepy.Cursor(myapi.user_timeline,id="abby").items(2):
     print status.text+"/n"