-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuser_token.py
More file actions
39 lines (26 loc) · 1001 Bytes
/
user_token.py
File metadata and controls
39 lines (26 loc) · 1001 Bytes
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
import sys
import webbrowser
import tweepy
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
def generate_user_access_token():
"""
Generate a Twitter API connection with access for a specific user.
Requires the user to view the browser URI that is automatically opened,
then manually enter the pin in the command-line in order to generate
the access token.
:return: tweepy.OAuthHandler instance, with User Access Token set.
"""
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
print("You need to authorize the application. Opening page in browser...\n")
auth_url = auth.get_authorization_url()
webbrowser.open(auth_url)
user_pin = input("Generate a pin and enter it here, or type `quit`. /> ")
if not user_pin or user_pin.lower() in ("q", "quit", "exit"):
print("Exiting.")
sys.exit(0)
print("Authenticating...")
auth.get_access_token(user_pin)
return auth
auth = generate_user_access_token()
tweepy.API(auth)