-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorize.py
43 lines (33 loc) · 1.25 KB
/
authorize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
Helper script that performs an OAuth 1a handshake to authorize a Twitter account to use this app and
returns an access token and secret that this app can use to act on behalf of said authorized user.
"""
import os
import tweepy
from dotenv import load_dotenv
#
# Load environment variables. We specifically need the app's consumer key and secret here.
#
load_dotenv()
TWITTER_CONSUMER_KEY = os.getenv('TWITTER_CONSUMER_KEY')
TWITTER_CONSUMER_SECRET = os.getenv('TWITTER_CONSUMER_SECRET')
#
# Open up a web browser where the Twitter account can be logged into.
#
auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
try:
redirect_url = auth.get_authorization_url()
except tweepy.TweepError:
print('Error! Failed to generate access request URL.')
print(f"STEP 1: Go to {redirect_url} and authorize the desired Twitter account.")
verifier_code = input("STEP 2: Enter Twitter authorization PIN: ")
#
# Exchange the verifier code for an access token and secret.
#
try:
auth.get_access_token(verifier_code)
except tweepy.TweepError:
print('Error! Failed to get access token and secret.')
print("----------------------")
print(f" Access Token: {auth.access_token}")
print(f" Access Token Secret: {auth.access_token_secret}")