This section is going to help you understand creating a Twitter Application, authenticating a user, and making basic API calls
Beginning¶First, youâll want to head over to https://apps.twitter.com/ and register an application!
After you register, grab your applications Consumer Key
and Consumer Secret
from the application details tab.
Now youâre ready to start authentication!
Authentication¶Twython offers support for both OAuth 1 and OAuth 2 authentication.
The difference:
Important
Again, if your web app is planning on using interacting with users, this IS the authentication type for you. If youâre not interested in authenticating a user and plan on making read-only calls, check out the OAuth 2 section.
First, youâll want to import Twython
from twython import Twython
Now, youâll want to create a Twython instance with your Consumer Key
and Consumer Secret
Note
Only pass callback_url to get_authentication_tokens if your application is a Web Application
Desktop and Mobile Applications do not require a callback_url
APP_KEY = 'YOUR_APP_KEY' APP_SECRET = 'YOUR_APP_SECRET' twitter = Twython(APP_KEY, APP_SECRET) auth = twitter.get_authentication_tokens(callback_url='http://mysite.com/callback')
From the auth
variable, save the oauth_token_secret
for later use (these are not the final auth tokens). In Django or other web frameworks, you might want to store it to a session variable
OAUTH_TOKEN = auth['oauth_token'] OAUTH_TOKEN_SECRET = auth['oauth_token_secret']
Send the user to the authentication url, you can obtain it by accessing
Handling the Callback¶Note
If your application is a Desktop or Mobile Application oauth_verifier will be the PIN code
After they authorize your application to access some of their account details, theyâll be redirected to the callback url you specified in get_autentication_tokens
Youâll want to extract the oauth_verifier
from the url.
Django example:
oauth_verifier = request.GET['oauth_verifier']
Now that you have the oauth_verifier
stored to a variable, youâll want to create a new instance of Twython and grab the final user tokens
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) final_step = twitter.get_authorized_tokens(oauth_verifier)
Once you have the final user tokens, store them in a database for later use!
OAUTH_TOKEN = final_step['oauth_token'] OAUTH_TOKEN_SECRET = final_step['oauth_token_secret']OAuth 2 (Application Authentication)¶
Attention
Just a reminder, this authentication type is for when you donât want to authenticate and interact with users and make read-only calls to Twitter
OAuth 2 authentication is 100x easier than OAuth 1. Letâs say you just made your application and have your Consumer Key
and Consumer Secret
First, youâll want to import Twython
from twython import TwythonObtain an OAuth 2 Access Token¶
APP_KEY = 'YOUR_APP_KEY' APP_SECRET = 'YOUR_APP_SECRET' twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2) ACCESS_TOKEN = twitter.obtain_access_token()
Save ACCESS_TOKEN
in a database or something for later use!
APP_KEY = 'YOUR_APP_KEY' ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN' twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN)
Now that you have your OAuth 2 access_token, maybe youâll want to perform a search or something
The Twython API Table¶The Twython package contains a file endpoints.py
which holds a Mixin of all Twitter API endpoints. This is so Twythonâs core api.py
isnât cluttered with 50+ methods.
Keyword arguments to functions are mapped to the functions available for each endpoint in the Twitter API docs. Doing this allows us to be incredibly flexible in querying the Twitter API, so changes to the API arenât held up from you using them by this library.
What Twython Returns¶Twython returns native Python objects. We convert the JSON sent to us from Twitter to an object so you donât have to.
Now that you have a little idea of the type of data youâll be receiving, briefed on how arguments are handled, and your application tokens and user oauth tokens (or access token if youâre using OAuth 2), check out the basic usage section.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4