3

I'm unsure what is going wrong in here. The following code was working until 2 days ago. I'm using tweepy version number 3.6.0 on python3 in jupyter notebook. Now, when I execute the code given below, I keep getting the error, TweepError: [{'code': 215, 'message': 'Bad Authentication data.'}]. What am I doing wrong? I've already looked at similar posts 1, 2,3,4,5 and 6 but no solution. Note, I have also regenerated the keys, still the error persists.

The code is;

import tweepy
ckey = 'xxx'
csecret = 'xxx'
atoken = 'xxx'
asecret = 'xxx'

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
# collect tweets on #MRT
for tweet in tweepy.Cursor(api.search,q="MRT",count=100,
                           lang="en",rpp=100,
                           since="2017-04-03").items():
  print (tweet.created_at, tweet.text)

On executing this code returns the following errors,

---------------------------------------------------------------------------
TweepError                                Traceback (most recent call last)
<ipython-input-11-b3e4ffb2d94f> in <module>()
      2 for tweet in tweepy.Cursor(api.search,q="MRT",count=100,
      3                            lang="en",rpp=100,
----> 4                            since="2017-04-03").items():
      5     print (tweet.created_at, tweet.text)

~\Miniconda3\lib\site-packages\tweepy\cursor.py in __next__(self)
     47 
     48     def __next__(self):
---> 49         return self.next()
     50 
     51     def next(self):

~\Miniconda3\lib\site-packages\tweepy\cursor.py in next(self)
    195         if self.current_page is None or self.page_index == len(self.current_page) - 1:
    196             # Reached end of current page, get the next page...
--> 197             self.current_page = self.page_iterator.next()
    198             self.page_index = -1
    199         self.page_index += 1

~\Miniconda3\lib\site-packages\tweepy\cursor.py in next(self)
    106 
    107         if self.index >= len(self.results) - 1:
--> 108             data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kargs)
    109 
    110             if hasattr(self.method, '__self__'):

~\Miniconda3\lib\site-packages\tweepy\binder.py in _call(*args, **kwargs)
    248             return method
    249         else:
--> 250             return method.execute()
    251 
    252     # Set pagination mode

~\Miniconda3\lib\site-packages\tweepy\binder.py in execute(self)
    232                     raise RateLimitError(error_msg, resp)
    233                 else:
--> 234                     raise TweepError(error_msg, resp, api_code=api_error_code)
    235 
    236             # Parse the response payload

TweepError: Twitter error response: status code = 400

Any advice will be helpful.

maverick
  • 315
  • 2
  • 10
  • Can you try running the code without the `since` attribute in the `tweepy.Cursor()` method? – Soumithri Chilakamarri Sep 05 '18 at 07:44
  • @soumithri-chilakamarri I removed the `since` attribute as advised. Now, I get a new error, `TweepError: Twitter error response: status code = 400`. I've checked these related posts [1](https://stackoverflow.com/questions/28124318/tweepy-error-response-status-code-400) and [2](https://stackoverflow.com/questions/52045599/tweepy-error-code-400) on this error, still no solution. – maverick Sep 05 '18 at 07:55

3 Answers3

2

I had the same problem. Regenerating the Keys and tokens solved the problem.

1

I solved the problem using double quotes, I see that you used single quotes

CONSUMER_KEY = "xxx";

0

I copied your code and executed it in my system and I was not able to find any errors. I'm using tweepy 3.6.0 and Python 3.5.2. There are two edits that I have done to your code.

import tweepy
ACCESS_TOKEN = "#####"
ACCESS_TOKEN_SECRET = "#####"
CONSUMER_KEY = "####" 
CONSUMER_SECRET = "#####"

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
# Creation of the actual interface, using authentication
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
# collect tweets on     #MRT
for tweet in tweepy.Cursor(api.search,q="MRT",count=100,
                       lang="en",rpp=100).items():
    print (tweet.created_at, tweet.text)

Notice the two parameters tweepy.API: wait_on_rate_limit and wait_on_rate_limit_notify. These two parameters are important if you want to keep the tweets streaming on because the search API only gives you a certain amount of tweets per request.

You have a TweepError with status code 400. According to the documentation, it says:

The request was invalid or cannot be otherwise served. An accompanying error message will explain further. Requests without authentication are considered invalid and will yield this response.

A Possible explanation is that the your twitter API keys do not authenticate anymore because you have been requesting beyond the rate limits of twitter.

Hopefully this helps.

  • thank you for the detailed answer. It was helpful but the error still persists. Could it be because this app was initially created on `https://apps.twitter.com/`. Until 2 days ago, I was using this website.Then I received a notification about something like the website will be retired and to make a move to `https://developer.twitter.com/`. So I've applied for the developer account. My old apps are now on the new site. And since then on executing the code, I keep getting `TweepError: Twitter error response: status code = 400`. Any suggestion will be very helpful. – maverick Sep 06 '18 at 02:16
  • How long does it take for the developer account to be approved on this new site, `https://developer.twitter.com/`? Any idea's? – maverick Sep 06 '18 at 02:19
  • So it appears that the `user` and `site` streaming `API's` are replaced with `Account Activity API`. For using this API, you are supposed to have a premium or enterprise account. More details here: https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/migration/us-ss-migration-guide – Soumithri Chilakamarri Sep 06 '18 at 02:39
  • Can you try to check if you are getting a response by using `curl` method present in this link --> https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html – Soumithri Chilakamarri Sep 06 '18 at 02:46
  • 1
    For anyone reading this post, please note it has nothing to do with the website migration to `developer.twitter.com`. @soumithri-chilakamarri I found the mistake. The mistake was I'd put the keys in a text file where I enclosed them within single quotes. I was then reading this text file in my program. I removed the single quotes wrapped around the keys, read the text file in the program and viola, the program works. Thanks a lot. I'have marked your response as an answer. – maverick Sep 06 '18 at 02:49
  • Thank you. Glad you resolved it and my mistake to think that it needs to do with migration. – Soumithri Chilakamarri Sep 06 '18 at 02:55