This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenAPI.py
75 lines (54 loc) · 1.99 KB
/
TokenAPI.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
import requests
class TokenAPI:
def __init__(self, api_key, api_secret):
self.api_key = api_key
self.api_secret = api_secret
def new_token(self):
url = 'https://api.twitter.com/oauth2/token'
params = {
'grant_type': 'client_credentials',
}
auth = ( self.api_key, self.api_secret )
r = requests.post(url, params=params, auth=auth)
if r.status_code != 200:
raise Exception(r.text)
data = r.json()
token_type = data['token_type']
assert token_type == 'bearer', 'Invalid token_type: %s' % token_type
access_token = data['access_token']
return access_token
def revoke_token(self, access_token):
url = 'https://api.twitter.com/oauth2/invalidate_token'
params = {
'access_token': access_token,
}
auth = ( self.api_key, self.api_secret )
r = requests.post(url, params=params, auth=auth)
if r.status_code != 200:
raise Exception(r.text)
data = r.json()
revoked_token = data['access_token']
assert revoked_token == access_token
return revoked_token
if __name__ == '__main__':
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('type', type=str, choices=[ 'new', 'revoke', ])
args = parser.parse_args()
api_key = os.environ.get('API_KEY')
api_secret = os.environ.get('API_SECRET')
if not api_key:
api_key = input('API Key: ')
if not api_secret:
api_secret = input('API Secret: ')
api = TokenAPI(api_key=api_key, api_secret=api_secret)
if args.type == 'new':
token = api.new_token()
print('New Access Token: %s' % token)
elif args.type == 'revoke':
token = os.environ.get('ACCESS_TOKEN')
if not token:
token = input('Token to revoke: ')
revoked_token = api.revoke_token(access_token=token)
print('Revoked Token: %s' % revoked_token)