-1

Tweepy API Request to twitter return me Twitter error response: status code=401.

This is my actual code:

def func():
    tweets = list(tweepy.Cursor(
        api.search, "{0}".format("COVID-19"), lang="en",tweet_mode='extended', count=5).items(7))
    Tweet_outpt = []
    for i in tweets:
        text = i.full_text
        place = ""
        if i.place:
            place = i.place.full_name
        json_output = {
            "tweet": text,
        }
        Tweet_outpt.append(json_output)
    return Tweet_outpt
final_arr=func()
df = pd.DataFrame(final_arr)
df
TweepError                                Traceback (most recent call last)
<ipython-input-2-f192d2ae6519> in <module>
     13         Tweet_outpt.append(json_output)
     14     return Tweet_outpt
---> 15 final_arr=func()
     16 df = pd.DataFrame(final_arr)
     17 df

5 frames
/usr/local/lib/python3.7/dist-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 = 401

I have been tried to scrap the tweets using tweepy package got all the required keys. Is tweepy package will not work? Could anyone help me resolve this issue.

juliomalves
  • 42,130
  • 20
  • 150
  • 146
Athiban
  • 1
  • 2
  • Can you provide the code where the error is ocurring? – Rodrigo Guzman Oct 28 '22 at 15:44
  • try these solutions [solution](https://stackoverflow.com/questions/28412683/401-error-when-retrieving-twitter-data-using-tweepy) – anon-bee-01 Oct 28 '22 at 15:47
  • 1
    Please just use the [edit](https://stackoverflow.com/posts/74237772/edit) function. Don't post additional information to your question as comments as this is highly unreadable and can easily get lost. – QBrute Oct 28 '22 at 15:47

1 Answers1

0

Code 401 is an authentication error, please make sure you're using the correct tokens.

Tweepy uses different syntax depending on if you're using Twitter's API V1 or V2.

If you're using V2 use the bearer token to set up your Client object.

client = tweepy.Client(bearer_token=("token"))

just replacing the "token" string for your token

Rodrigo Guzman
  • 487
  • 2
  • 9
  • I have just using consumer_key , consumer_secret , access_token, access_token_secret but I having the bearer_token but haven't tried with that. – Athiban Oct 28 '22 at 15:59