I am trying to pull tweets from Twitter API meeting a certain criteria… I have Elevated access for my relevant project. Here is my script:

# import tweepy
import tweepy as tw

# your Twitter API key and API secret
consumer_key = "CONSUMER_API_KEY"
consumer_secret = "CONSUMER_API_SECRET"
access_key = "API_ACCESS_KEY"
access_secret = "API_ACCESS_SECRET"

# authenticate
auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tw.API(auth, wait_on_rate_limit=True)

search_query = "#ABCDstudy --filter:retweets"

# get tweets from the API, might need to loop for various languages? 

tweets = tw.Cursor(api.search_tweets,
             q=search_query,
             lang="en").items()
# store the API responses in a list
tweets_copy = []

for tweet in tweets:
   tweets_copy.append(tweet)
  
print("Total Tweets fetched:", len(tweets_copy))

Here is the error:

---------------------------------------------------------------------------
Forbidden                                 Traceback (most recent call last)
Input In [9], in <cell line: 9>()
      6 # store the API responses in a list
      7 tweets_copy = []
----> 9 for tweet in tweets:
     10    tweets_copy.append(tweet)
     12 print("Total Tweets fetched:", len(tweets_copy))

File ~/Library/jupyterlab-desktop/jlab_server/lib/python3.8/site-packages/tweepy/cursor.py:86, in BaseIterator.__next__(self)
     85 def __next__(self):
---> 86     return self.next()

File ~/Library/jupyterlab-desktop/jlab_server/lib/python3.8/site-packages/tweepy/cursor.py:286, in ItemIterator.next(self)
    283     raise StopIteration
    284 if self.current_page is None or self.page_index == len(self.current_page) - 1:
    285     # Reached end of current page, get the next page...
--> 286     self.current_page = next(self.page_iterator)
    287     while len(self.current_page) == 0:
    288         self.current_page = next(self.page_iterator)

File ~/Library/jupyterlab-desktop/jlab_server/lib/python3.8/site-packages/tweepy/cursor.py:86, in BaseIterator.__next__(self)
     85 def __next__(self):
---> 86     return self.next()

File ~/Library/jupyterlab-desktop/jlab_server/lib/python3.8/site-packages/tweepy/cursor.py:167, in IdIterator.next(self)
    164     raise StopIteration
    166 if self.index >= len(self.results) - 1:
--> 167     data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kwargs)
    169     model = ModelParser().parse(
    170         data, api = self.method.__self__,
    171         payload_list=self.method.payload_list,
    172         payload_type=self.method.payload_type
    173     )
    174     result = self.method.__self__.parser.parse(
    175         data, api = self.method.__self__,
    176         payload_list=self.method.payload_list,
    177         payload_type=self.method.payload_type
    178     )

File ~/Library/jupyterlab-desktop/jlab_server/lib/python3.8/site-packages/tweepy/api.py:33, in pagination.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
     31 @functools.wraps(method)
     32 def wrapper(*args, **kwargs):
---> 33     return method(*args, **kwargs)

File ~/Library/jupyterlab-desktop/jlab_server/lib/python3.8/site-packages/tweepy/api.py:46, in payload.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
     44 kwargs['payload_list'] = payload_list
     45 kwargs['payload_type'] = payload_type
---> 46 return method(*args, **kwargs)

File ~/Library/jupyterlab-desktop/jlab_server/lib/python3.8/site-packages/tweepy/api.py:1303, in API.search_tweets(self, q, **kwargs)
   1209 @pagination(mode='id')
   1210 @payload('search_results')
   1211 def search_tweets(self, q, **kwargs):
   1212     """search_tweets(q, *, geocode, lang, locale, result_type, count, \
   1213                      until, since_id, max_id, include_entities)
   1214 
   (...)
   1301     .. _Twitter's documentation on the standard search API: https://developer.twitter.com/en/docs/twitter-api/v1/tweets/search/overview
   1302     """
-> 1303     return self.request(
   1304         'GET', 'search/tweets', endpoint_parameters=(
   1305             'q', 'geocode', 'lang', 'locale', 'result_type', 'count',
   1306             'until', 'since_id', 'max_id', 'include_entities'
   1307         ), q=q, **kwargs
   1308     )

File ~/Library/jupyterlab-desktop/jlab_server/lib/python3.8/site-packages/tweepy/api.py:259, in API.request(self, method, endpoint, endpoint_parameters, params, headers, json_payload, parser, payload_list, payload_type, post_data, files, require_auth, return_cursors, upload_api, use_cache, **kwargs)
    257     raise Unauthorized(resp)
    258 if resp.status_code == 403:
--> 259     raise Forbidden(resp)
    260 if resp.status_code == 404:
    261     raise NotFound(resp)

Forbidden: 403 Forbidden
453 - You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve

I do have Elevated Access as you can see in the photo linked here:

Any ideas?

Thanks,

Clare

Just to clarify, you intend on calling the v1.1 Search API, not the v2 one?

To enable v1.1 API access on your app, make sure the project lists the app as the “Production” app. too.

However, if you intend to use the v2 Search API, this is not correct:

You should be using Client — tweepy 4.10.1 documentation instead

1 Like