generated from freelawproject/new-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathget-twitter-keys.py
77 lines (61 loc) · 2.5 KB
/
get-twitter-keys.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import environ
from requests_oauthlib import OAuth1Session
env = environ.FileAwareEnv()
CONSUMER_KEY = env("TWITTER_CONSUMER_KEY")
CONSUMER_SECRET = env("TWITTER_CONSUMER_SECRET")
REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token?oauth_callback=oob&x_auth_access_type=write"
BASE_AUTHORIZATION_URL = "https://api.twitter.com/oauth/authorize"
ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token"
def main():
if not CONSUMER_KEY or not CONSUMER_SECRET:
raise Exception(
"Please check your env file and make sure TWITTER_CONSUMER_KEY and TWITTER_CONSUMER_SECRET are set"
)
oauth = OAuth1Session(CONSUMER_KEY, client_secret=CONSUMER_SECRET)
try:
fetch_response = oauth.fetch_request_token(REQUEST_TOKEN_URL)
except ValueError:
print(
"There may have been an issue with the consumer_key or consumer_secret you entered."
)
resource_owner_key = fetch_response.get("oauth_token")
resource_owner_secret = fetch_response.get("oauth_token_secret")
authorization_url = oauth.authorization_url(BASE_AUTHORIZATION_URL)
print(f"Please go here and authorize: {authorization_url}")
verifier = input("Paste the PIN here: ")
oauth = OAuth1Session(
CONSUMER_KEY,
client_secret=CONSUMER_SECRET,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=verifier,
)
oauth_tokens = oauth.fetch_access_token(ACCESS_TOKEN_URL)
access_token = oauth_tokens["oauth_token"]
access_token_secret = oauth_tokens["oauth_token_secret"]
# Make the request
oauth = OAuth1Session(
CONSUMER_KEY,
client_secret=CONSUMER_SECRET,
resource_owner_key=access_token,
resource_owner_secret=access_token_secret,
)
fields = ""
params = {"user.fields": fields}
response = oauth.get("https://api.twitter.com/2/users/me", params=params)
if response.status_code != 200:
raise Exception(
f"Request returned an error: {response.status_code} {response.text}"
)
data = response.json()["data"]
print(
"\nUse the admin panel to create a new channel with the following data:"
)
print("Service: Twitter")
print(f"Account: {data['username']}")
print(f"Account id: {data['id']}")
print("Enable: True")
print(f"Access Token: {repr(access_token)[1:-1]}")
print(f"Access Token secret: {repr(access_token_secret)[1:-1]}")
if __name__ == "__main__":
main()