Search code examples
pythonapicsvtwittertweets

How to fix error code 403 on Twitter's 30 day sandbox search?


I am trying to collect tweets from Twitter's 30-day sandbox search API and write them into a csv file. I am getting error code 403 but I cannot gather any helpful information from the error message. I looked at related posts and I have either already tried what they said fixed their problem, or they were trying to do something completely different (error code 403 seems to be a blanket error code for many types of problems)

I have been able to collect tweets from the standard search but I need more data than what is available in just the last 7 to 9 days. I have a developer account with Twitter and on my dashboard, it says I still have 250 requests and 1M tweets for this month on the 30-day search sandbox (I have used 0)

Below is my code:

import csv
import settings as sett #file that includes my access token, consumer key and the secrets of each.
from TwitterAPI import TwitterAPI


api = TwitterAPI(sett.consumer_key, sett.consumer_secret, sett.access_token, sett.access_token_secret)
r = api.request('tweets/search/30day/:maruchan.json', {'query' : 'maruchan'})
                                     #^.......^To be honest I don't know what is supposed to go here, I just put something random, this might be the problem....

csvFile = open('maruchan.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)

for tweet in r:

    try:
        #Write a row to the csv file/ I use encode utf-8
        csvWriter.writerow([tweet.created_at, tweet.full_text.encode('utf-8'), tweet.favorite_count, tweet.retweet_count, tweet.id, tweet.user.screen_name])
    except tweepy.TweepError:
        pass
csvFile.close()

When I run this in my Jupyter notebook I get the following error:

---------------------------------------------------------------------------
TwitterRequestError                       Traceback (most recent call last)
<ipython-input-122-0fe9d55ba54b> in <module>
     15 csvWriter = csv.writer(csvFile)
     16 
---> 17 for tweet in r:
     18 
     19     try:

~\Anaconda3\lib\site-packages\TwitterAPI\TwitterAPI.py in __iter__(self)
    217         :raises: TwitterConnectionError, TwitterRequestError
    218         """
--> 219         return self.get_iterator()
    220 
    221     def get_quota(self):

~\Anaconda3\lib\site-packages\TwitterAPI\TwitterAPI.py in get_iterator(self)
    204         """
    205         if self.response.status_code != 200:
--> 206             raise TwitterRequestError(self.response.status_code)
    207 
    208         if self.stream:

TwitterRequestError: Twitter request failed (403)

I am very new to coding and don't know what I am doing, but I have not been able to figure this out on my own so I would appreciate if someone could help me out. Thanks!


Solution

  • I figured out what my problem was. The endpoint was wrong and the inputs to csvWriter.writerow were also wrong.

    Endpoint: For the endpoint, your environment name is found by going to your twitter developer account, in the top right-hand corner there is a drop-down menu with your username on it, click on "Dev environments". This should take you to a list of your Dev environments under each one should be "Dev environment label" (in my case that label was "research"). Take the label and put it into your API request with a colon preceding it and no ".json" after. Note: for the full archive search, simply replace "30day" with "fullarchive".

    CSV inputs: For the CSV inputs, they need to be in the format of tweet['aspect'] where tweet is an arbitrary variable name for us to iterate over and 'aspect' is the type of data requested (e.g. 'text' or 'created_at')

    Below is the correct code along with some notes.

    import csv
    import settings as sett #file that includes my access token, consumer key and the secrets of each. You get these by applying for a developer account and making an app
    from TwitterAPI import TwitterAPI
    
    
    api = TwitterAPI(sett.consumer_key, sett.consumer_secret, sett.access_token, sett.access_token_secret) #tells the Twitter API who you are.
    r = api.request('tweets/search/30day/:research', {'query' : 'maruchan lang:en', #maruchan is the search term and lang:en filters for english results
                                                      "maxResults": "100", #number of tweets collected (starting from newer tweets)
                                                      "fromDate":"201906250000", #start data
                                                      "toDate":"201907010000" #end data (tweets will only be collected between these dates)
                                                               #YYYYMMDDHHmm #format of the fromDate and toDate input values
                                                      })
    
    csvFile = open('maruchan_30_day_sandbox.csv', 'a') #'maruchan_30_day_sandbox.csv' is file name, 'a' is for append mode
    #Use csv Writer
    csvWriter = csv.writer(csvFile)
    
    for tweet in r:
        #creates csv file with the listed elements seperated by a comma in each row         #encode('utf-8') helps read certain characters
        csvWriter.writerow([tweet['created_at'], tweet['user']['screen_name'], tweet['text'].encode('utf-8') if 'text' in tweet else tweet])
    csvFile.close()