-
Notifications
You must be signed in to change notification settings - Fork 3
Method: Authentication
There are two different types of Oauth methods, namely OAuth 1.0a (User Context) and OAuth 2.0 Bearer Token (App Context). The rate limit is based on the authentication type you choose. The best place to see these comparisons is under the Migrate pages for a given endpoint in Twitter's documentation. Here is an example for the User Lookup endpoints.
OAuth 1a requires your user context Twitter keys/tokens (i.e. api_key
, api_key_secret
, access_token
, access_token_secret
).
You can create an OAuth1a
object using the following code:
import osometweet
api_key = "YOUR_TWITTER_API_KEY"
api_key_secret = "YOUR_TWITTER_API_KEY_SECRET"
access_token = "YOUR_TWITTER_ACCESS_TOKEN"
access_token_secret = "YOUR_TWITTER_ACCESS_TOKEN_SECRET"
oauth1a = osometweet.OAuth1a(
api_key=api_key,
api_key_secret=api_key_secret,
access_token=access_token,
access_token_secret=access_token_secret
)
Hint: You can set your Twitter credentials in your terminal by using the export
command like so...
export 'TWITTER_API_KEY'='YOUR_TWITTER_API_KEY'
export 'TWITTER_API_KEY_SECRET'='YOUR_TWITTER_API_KEY_SECRET'
...
You can do this for all of your keys/tokens and then import them to your Python environment by using the os
package in the following way.
api_key = os.environ.get("TWITTER_API_KEY")
api_key_secret = os.environ.get("TWITTER_API_KEY_SECRET")
...
This is valuable because you can then leave your keys/tokens - which should always be kept private - out of your code. This allows you to write code which is easier to share with others.
OAuth 2 only requires your bearer token. You can create an OAuth2
object with the folliwng code:
import osometweet
bearer_token = "YOUR_TWITTER_BEARER_TOKEN"
oauth2 = osometweet.OAuth2(bearer_token=bearer_token)
Before making use of osometweet
to gather data, you must first initialize the OsomeTweet
class. When initializing the OsomeTweet
class, you must provide a valid OAuth object in order to access Twitter's data.
In the rest of the examples, we will use OAuth2
by default, but you can replace it with OAuth1a
.
import osometweet
bearer_token = "YOUR_TWITTER_BEARER_TOKEN"
oauth2 = osometweet.OAuth2(bearer_token=bearer_token)
ot = osometweet.OsomeTweet(oauth2)
Now you can use the ot
instance to query the API.
osometweet
also handles Twitter's rate limiting errors for you by default.
Note: The rate limit manager does not work with the streaming endpoints.
If you do not want to utilize this feature, you must include manage_rate_limits=False
during authorization. For example...
import osometweet
bearer_token = "YOUR_TWITTER_BEARER_TOKEN"
oauth2 = osometweet.OAuth2(
bearer_token=bearer_token,
manage_rate_limits=False # <~~~ include this to turn off the rate limit manager
)
OR
import osometweet
api_key = "YOUR_TWITTER_API_KEY"
api_key_secret = "YOUR_TWITTER_API_KEY_SECRET"
access_token = "YOUR_TWITTER_ACCESS_TOKEN"
access_token_secret = "YOUR_TWITTER_ACCESS_TOKEN_SECRET"
oauth1a = osometweet.OAuth1a(
api_key=api_key,
api_key_secret=api_key_secret,
access_token=access_token,
access_token_secret=access_token_secret,
manage_rate_limits=False # <~~~ include this to turn off the rate limit manager
)
V2 API, by default, only returns limited information.
To fetch more, you will need to specify the fields
and expansions
parameters in the requests.
OSoMeTweet
contains several classes to handle them.
See Method: Specifying fields and expansions for examples of how to work with them.