-
Notifications
You must be signed in to change notification settings - Fork 2
/
oauth2cli.py
88 lines (80 loc) · 2.94 KB
/
oauth2cli.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
78
79
80
81
82
83
84
85
86
87
88
"""
to run it:
sudo pip install oauth2client
python oauth2cli.py --auth_host_port 9000 --client-secret <client-secret> --client-id <client-id>
it creates a json file called "credentials_sdm"
the access_token in the credentials is also printed to stdout
"""
from oauth2client.client import OAuth2WebServerFlow, OAuth2Credentials
from oauth2client.file import Storage
import argparse
from oauth2client import tools
import contextlib
import sys
import cStringIO
import httplib2
@contextlib.contextmanager
def nostdout():
save_stdout = sys.stdout
sys.stdout = cStringIO.StringIO()
yield
sys.stdout = save_stdout
def main(args):
flow = OAuth2WebServerFlow(client_id=args.client_id,
client_secret=args.client_secret,
scope='https://www.googleapis.com/auth/userinfo.email')
storage = Storage(args.filename)
credentials = tools.run_flow(flow, storage, args)
return credentials
def refresh(args):
try:
with open(args.filename, 'rU') as f:
credentials = OAuth2Credentials.from_json(f.read())
except IOError:
print >> sys.stderr, 'file', args.filename, 'does not exist'
return
if credentials.invalid:
print >> sys.stderr, 'invalid_grant: Token has been revoked.'
return
if credentials.access_token_expired:
try:
credentials.refresh(httplib2.Http())
storage = Storage(args.filename)
storage.put(credentials)
credentials.set_store(storage)
except Exception as e:
print >> sys.stderr, e
return
return credentials
def revoke(args):
try:
with open(args.filename, 'rU') as f:
credentials = OAuth2Credentials.from_json(f.read())
except IOError:
print >> sys.stderr, 'file', args.filename, 'does not exist'
return
try:
credentials.revoke(httplib2.Http())
storage = Storage(args.filename)
storage.put(credentials)
credentials.set_store(storage)
except Exception as e:
print >> sys.stderr, e
return
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--filename', type=str, default='credentials_sdm')
subparsers = parser.add_subparsers()
subparser_create = subparsers.add_parser('create', help='create a new token', parents=[tools.argparser])
subparser_create.add_argument('--client-id', type=str, required=True)
subparser_create.add_argument('--client-secret', type=str, required=True)
subparser_create.set_defaults(func=main)
subparser_refresh = subparsers.add_parser('refresh', help='refresh an existing token')
subparser_refresh.set_defaults(func=refresh)
subparser_refresh = subparsers.add_parser('revoke', help='revoke a token')
subparser_refresh.set_defaults(func=revoke)
args = parser.parse_args()
with nostdout():
c = args.func(args)
if c:
print c.access_token