Then you can import the values and use them in other scripts.
app.py
import config_local
assert config_local.CONSUMER_KEY and config_local.CONSUMER_SECRET,"Consumer credentials must be set"assert config_local.ACCESS_KEY and config_local.ACCESS_SECRET,"Access credentials must be set"
If using git or Github for your project, make sure to never includes these in version control (commits). They can be stored in an unversioned config file ignored by .gitignore, as covered in this tutorial. Using environment variables.
macOS and Linux users: Avoid storing your credentials in a global config file such as .bashrc and setting them with export, as then those are available to every application that runs on your machine which is not secure.
CONSUMER_KEY = os.environ.get('CONSUMER_KEY')
CONSUMER_SECRET = os.environ.get('CONSUMER_SECRET')
ACCESS_KEY = os.environ.get('ACCESS_KEY')
ACCESS_SECRET = os.environ.get('ACCESS_SECRET')assert CONSUMER_KEY and CONSUMER_SECRET,"Consumer credentials must be set"assert ACCESS_KEY and ACCESS_SECRET,"Access credentials must be set"
Authenticate with Twitter API as your own account but with user context
Here we authorize with an App Access Token approach, the typical flow for authorizing so you can fetch data and do automated tasks like tweet as the account which you authenticated with.
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
Tweepy docs See the parameters that API takes and what they mean at this reference.
You can continue with the steps here to make the api object more robust or add go to the Token types section to explore other options. Or stick with your current api object setup and jump to Tweepy code samples section and start requesting the API.
You don't have to worry about waiting when rate limit is reached. Use wait_on_rate_limit to enable this and wait_on_rate_limit_notify to get notified on the console that the app is waiting.
Note: Some codes like 429 for Too Many Requests can be left out if the Tweepy rate limit waiting will handle them already. Also, forcing a retry immediately when rate limiting could be a bad idea, so don't put 429 above.
Put the logic above in a function. This makes keeps the values out of the global scope and it means it is easy to import and use the function in multiple scripts.
defget_api_connection(
consumer_key, consumer_secret, access_key=None, access_secret=None):"""
Authorize with Twitter and return API connection object.
"""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)if access_key and access_secret:
auth.set_access_token(access_key, access_secret)
api = tweepy.API(
auth,
wait_on_rate_limit=True,
wait_on_rate_limit_notify=True,
retry_count=3,
retry_delay=5,
retry_errors=[401,404,500,503],)return api
Example use:
api = get_api_connection(
CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_KEY,
ACCESS_SECRET
)
api.verify_credentials()print(api.me())
The current user's profile is now "me", so getting "my" tweets or followers is relative to that account.
You can tweet and favorite on their behalf.
Twitter Policies- please see this guide before doing actions such as tweeting, favoriting or doing replies or direct messages. As these are strictly controlled and you may get your account blocked or your dev application refused.
This is useful for automated tasks like searches. This method does not have a sense of a user context so get it can't get its own timeline or make a tweet. It is can be used for pulling data for known tweet IDs or users or for doing searches. It does have relaxed rate limits - in particular the search API with this token lets you do 480 requests per window rather than 180. Read more on the Twitter policies page.
As this method is specific to the application, it does not involve any users. This method is typically for developers that need read-only access to public information.
API calls using app-only authentication are rate limited per API method at the app level. source
Implement follow the user sign-in flow to act as another user on their behalf, but without seeing their password.
This flow is complex and is not necessary for doing searches or making a bot. But is necessary if you want to perform actions on behalf of the user with their permission (such as a liking a Tweet in a mobile app you made).
The user will sign into Twitter in the browser and then will get a short number to enter in your app.This use flow would require you to setup your own API to handle this complex flow. Or you can enter the code on the command-line and capture using input() if you want to try that out locally without the extra setup.
Rate limiting is measured on each user token, not your application. This can be useful do perform many actions as various users which would exceed the limits of a single account. However, note that you cannot cheat this as you need those unique to actually sign into your application.
Note that you should not sign in your application with this flow as your own account, as it will reset the consumer credentials. Then you'll have to copy them from your dev account settings again and then use them in your application. So rather have one Twitter account which you use for automation and one Twitter which you use for normal activity, then test your user sign-in flow with your real account.
I've used this flow before and got it to work. But I had no use-case so I stopped using it and it may be out of date with Tweepy or Twitter API changes.
Note you'll also have to setup the auth URL on your Twitter dev account.
user_token.py
import sys
import webbrowser
import tweepy
CONSUMER_KEY =""
CONSUMER_SECRET =""defgenerate_user_access_token():"""
Generate a Twitter API connection with access for a specific user.
Requires the user to view the browser URI that is automatically opened,
then manually enter the pin in the command-line in order to generate
the access token.
:return: tweepy.OAuthHandler instance, with User Access Token set.
"""
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)print("You need to authorize the application. Opening page in browser...\n")
auth_url = auth.get_authorization_url()
webbrowser.open(auth_url)
user_pin =input("Generate a pin and enter it here, or type `quit`. /> ")ifnot user_pin or user_pin.lower()in("q","quit","exit"):print("Exiting.")
sys.exit(0)print("Authenticating...")
auth.get_access_token(user_pin)return auth
auth = generate_user_access_token()
tweepy.API(auth)