python-twitter
raw JSON → 3.5 verified Mon Apr 27 auth: no python
A Python wrapper around the Twitter API. Version 3.5 fixes DM endpoint handling. Release cadence is sporadic.
pip install python-twitter Common errors
error ImportError: cannot import name 'Api' from 'twitter' ↓
cause Incorrect import path - trying to import from the top-level 'twitter' package instead of direct import.
fix
Use 'from twitter import Api' (not 'import twitter' then 'twitter.Api').
error twitter.error.TwitterError: [{u'message': u'Could not authenticate you', u'code': 32}] ↓
cause Invalid or missing OAuth credentials.
fix
Double-check your consumer key/secret and access token/secret. Ensure they are for the same Twitter App and user.
Warnings
breaking In v3.0+, Python 2 support was dropped. Ensure you are using Python 3. ↓
fix Use Python 3.6+.
deprecated Api.PostMedia() is deprecated and will be removed in a future version. Use Api.PostUpdate() with media parameter instead. ↓
fix Replace PostMedia() with PostUpdate() and pass media as an argument.
gotcha The library previously used a different OAuth flow. Make sure you are using the correct set of keys (API key, API secret key, Access token, Access token secret). ↓
fix Use consumer_key, consumer_secret, access_token_key, access_token_secret as shown in quickstart.
Imports
- Api wrong
from twitter.api import Apicorrectfrom twitter import Api
Quickstart
from twitter import Api
consumer_key = os.environ.get('TWITTER_CONSUMER_KEY', '')
consumer_secret = os.environ.get('TWITTER_CONSUMER_SECRET', '')
access_token_key = os.environ.get('TWITTER_ACCESS_TOKEN_KEY', '')
access_token_secret = os.environ.get('TWITTER_ACCESS_TOKEN_SECRET', '')
api = Api(consumer_key, consumer_secret, access_token_key, access_token_secret)
status = api.PostUpdate('Hello world!')
print(status.text)