Skip to content

Commit c592d81

Browse files
committed
Authenticate twitter api
1 parent a43cb96 commit c592d81

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

twitter_bot/twitter_api.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import tweepy
2+
3+
def get_twitter_api():
4+
consumer_key = '' # API key
5+
consumer_secret = '' # API secret key
6+
access_token = ''
7+
access_token_secret = ''
8+
9+
# Correcting the class name for the OAuth handler
10+
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
11+
auth.set_access_token(access_token, access_token_secret)
12+
13+
api = tweepy.API(auth)
14+
15+
# Attempt to use the API to confirm authentication works
16+
try:
17+
api.verify_credentials()
18+
print("Successfully authenticated with Twitter API.")
19+
except tweepy.TweepyException as e:
20+
print(f"Failed to authenticate with Twitter API: {e}")
21+
return None # Optionally return None or handle differently if auth fails
22+
23+
return api
24+
25+
# Call the function to test authentication
26+
api = get_twitter_api()

0 commit comments

Comments
 (0)