Hello everyone, i want to use the twitter api to retrieve tweets and forward them to my IA model for classification using Streamlit for the Web application , the issue is that i could retrieve data in an independent script with tweepy but when using the same lines of code in my streamlit script and running it I get the “401 Unauthorized” error

consumerKey = '#'
consumerSecret = '#'
accessToken = '#'
accessTokenSecret = '#'


#Create the authentication object
authenticate = tweepy.OAuthHandler(consumerKey, consumerSecret) 
    
# Set the access token and access token secret
authenticate.set_access_token(accessToken, accessTokenSecret) 
    
# Creating the API object while passing in auth information
api = tweepy.API(authenticate, wait_on_rate_limit = True)

if st.button("Analyze"):

			
			if Analyzer_choice == "Show Recent Tweets":

				st.success("Fetching last 5 Tweets")

				
				def Show_Recent_Tweets(raw_text):

					# Extract 100 tweets from the twitter user
					posts = api.user_timeline(screen_name=raw_text, count = 20, lang ="en", tweet_mode="extended")

					
					def get_tweets():

						l=[]
						i=1
						for tweet in posts[:5]:
							l.append(tweet.full_text)
							i= i+1
						return l

					recent_tweets=get_tweets()		
					return recent_tweets

				recent_tweets= Show_Recent_Tweets(raw_text)

				st.write(recent_tweets)

Unauthorized: 401 Unauthorized
Traceback:

File "D:\anaconda\envs\deeplearning\lib\site-packages\streamlit\scriptrunner\script_runner.py", line 443, in _run_script
    exec(code, module.__dict__)
File "C:\Users\hp\Desktop\Tweet-Analyzer-master\app.py", line 331, in <module>
    app()
File "C:\Users\hp\Desktop\Tweet-Analyzer-master\app.py", line 116, in app
    recent_tweets= Show_Recent_Tweets(raw_text)
File "C:\Users\hp\Desktop\Tweet-Analyzer-master\app.py", line 101, in Show_Recent_Tweets
    posts = api.user_timeline(screen_name=raw_text, count = 20, lang ="en", tweet_mode="extended")
File "D:\anaconda\envs\deeplearning\lib\site-packages\tweepy\api.py", line 33, in wrapper
    return method(*args, **kwargs)
File "D:\anaconda\envs\deeplearning\lib\site-packages\tweepy\api.py", line 46, in wrapper
    return method(*args, **kwargs)
File "D:\anaconda\envs\deeplearning\lib\site-packages\tweepy\api.py", line 571, in user_timeline
    return self.request(
File "D:\anaconda\envs\deeplearning\lib\site-packages\tweepy\api.py", line 257, in request
    raise Unauthorized(resp)

i’ve tried regenerating the tokens, using another app, using the bearer token instead ,synchin the clock… but the issue persist and seems to be that twitter is blocking me from using the api in streamlit only

what environment are you running the streamlit app in? is it still the same machine as your script or a different server?

Oh, also: api.user_timeline is a v1.1 API, not v2 - to use v1.1 API you need elevated access, so you likely have Basic, with v2 API only. You need Client — tweepy 4.8.0 documentation

first of all thank you for the reponse and yes i’m running the streamlit app in the same machine as localhost.

And i have an elevated access so it should work as i said

works fine using in it in an independent python script, but when it’s on streamlit it returns the error

1 Like

My guess would be that some part of the streamlit script isn’t the same as the independent script and something is missing? eg: Environment variable not read? Or the defined keys not being loaded or something? No idea what i could be.

1 Like

Could it be that twitter is detecting the streamlit running as me deploying the app and somehow blocks my access ?

Because the code in the independent script is pretty much the same, i even tried using someone else’s streamlit app (that is already working) with my own tokens and i still have the same issue

here’s content of the independent script when testing

import tweepy as tw

consumer_key = '#'
consumer_secret = '#'
access_token = '#'
access_token_secret = '#'
auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth)

public_tweets = [tweet for tweet in tw.Cursor(api.user_timeline, q= 'nayaR_dr', count = 50).items(1)]
for tweet in public_tweets:
    print(tweet.text)

There’s no way for twitter to tell how the code is running, but it may be a streamlit specific issue

1 Like