ptwobrussell / Mining-the-Social-Web-2nd-Edition

The official online compendium for Mining the Social Web, 2nd Edition (O'Reilly, 2013)
http://bit.ly/135dHfs
Other
2.9k stars 1.49k forks source link

cookbook ex: 1 - twitter-python oauth changes #333

Open skilfullycurled opened 6 years ago

skilfullycurled commented 6 years ago

Hi,

First, thank you for this great book. I've relied upon it quite a bit!

Second, after recently returning to using it, I found that the oauth_login function from example one in the cookbook was no longer working.

Turns out that twitter-python has changed its API to be more inline with Twitter's requirement that all API calls now be made with oAuth tokens.

Two things in particular: 1) twitter.oauth.OAuth has been replaced by twitter.Api. 2) the order/names of the tokens you pass are different. See below.

import twitter

def oauth_login():

    CONSUMER_KEY = 'your_consumer_key'
    CONSUMER_SECRET = 'your_consumer_secret'
    OAUTH_TOKEN_KEY = 'your_oauth_token_key'
    OAUTH_TOKEN_SECRET = 'your_oauth_token_secret'

    # UPDATED FOR twitter-python 2.0 ALL CALLS ARE OAUTH NOW
    twitter_api = twitter.Api(consumer_key=CONSUMER_KEY, \
                              consumer_secret=CONSUMER_SECRET, \
                              access_token_key=OAUTH_TOKEN_KEY, \
                              access_token_secret=OAUTH_TOKEN_SECRET)

    return twitter_api