Search code examples
apitwitteroauth-2.0tweepy

Do you need both the access token and consumer key to search Twitter?


I am a beginner at using the Twitter API. I have an app registered. I am following the tutorial for Tweepy. All I want to do for now is to search tweets. I don't need any permissions from others.

consumer_key = "xxxx"
consumer_secret = "xxx"
access_key = "xxxxxx"
access_secret = "xxxxxxx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

If understand correctly the consumer key and secret are my personal identifiers and the access token is the token the app obtains to get other user's permission. Please correct me if I am wrong.

Do I even need to use the access token to simple twitter search for myself?


Solution

  • Assuming you just want to use the Standard search API (/1.1/search/tweets.json), providing an access token and secret is not required. The following code works just fine without providing them:

    consumer_key = ""
    consumer_secret = ""
    
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    
    api = tweepy.API(auth)
    
    search = api.search("python")
    print(search)
    

    As per the documentation for this endpoint, you can choose to also interact with this endpoint acting as an actual Twitter account by providing an Access Token and Secret, but be aware that these have different rate limits:

    enter image description here

    Consumer Key + Secret = App-Based Authentication

    Access Token + Secret = User-Based Authentication

    And so just to clarify, your consumer key and consumer secret are identifiers for your app that you created and cannot perform actions as any Twitter account (including your own) without an access token and secret. So yes, the access token and secret allow you to authenticate as yourself or another user.