Getting a user twitter timeline in python

3.5k Views Asked by At

I need to get a users entire twitter timeline in python. I am using the following code with all the oauth keys:

import twitter
api = twitter.api(consumer_key='',
                consumer_secret='', access_token_key='', access_token_secret='')
statuses = api.GetUserTimeline(username)
print [s.text for s in statuses]

however, it returns [] for every account i try

I have no idea what im doing wrong. this is the first problem i've hade with python twitter.

2

There are 2 best solutions below

0
On

I think you have to change your second line as following:

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

There is no api class, instead there is Api. Please have a look at documentation.

0
On

Give it a try:

#!\usr\bin\env python

import twitter

def oauth_login():
    # credentials for OAuth
    CONSUMER_KEY = ''
    CONSUMER_SECRET = ''
    OAUTH_TOKEN = '' 
    OAUTH_TOKEN_SECRET = ''
    # Creating the authentification
    auth = twitter.oauth.OAuth( OAUTH_TOKEN,
                                OAUTH_TOKEN_SECRET,
                                CONSUMER_KEY,
                                CONSUMER_SECRET )
    # Twitter instance
    twitter_api = twitter.Twitter(auth=auth)
    return twitter_api

# LogIn
twitter_api = oauth_login()
# Get statuses
statuses = twitter_api.statuses.user_timeline(screen_name='SpongeBob')
# Print text 
print [status['text'] for status in statuses]