Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add save_token feature #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion python/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
import sys
import urllib.parse
import urllib.request

import os

This comment was marked as duplicate.

This comment was marked as off-topic.


def SetupOptionParser():
# Usage message is the module's docstring.
Expand Down Expand Up @@ -126,6 +126,11 @@ def SetupOptionParser():
dest='quiet',
help='Omit verbose descriptions and only print '
'machine-readable outputs.')
parser.add_option('--save_token',
action='store_true',
default=False,
dest='save_token',
help='persist token to disk (in current working directory)')
return parser


Expand Down Expand Up @@ -311,6 +316,10 @@ def main(argv):
RequireOptions(options, 'client_id', 'client_secret')
response = RefreshToken(options.client_id, options.client_secret,
options.refresh_token)
if options.save_token:
rt_filepath = os.path.join(os.getcwd(), 'refreshed_token.json')
with open(rt_filepath, 'w') as outfile:
json.dump(response, outfile)
if options.quiet:
print(response['access_token'])
else:
Expand All @@ -330,6 +339,10 @@ def main(argv):
authorization_code = input('Enter verification code: ')
response = AuthorizeTokens(options.client_id, options.client_secret,
authorization_code)
if options.save_token:
rt_filepath = os.path.join(os.getcwd(), 'initial_token.json')
with open(rt_filepath, 'w') as outfile:
json.dump(response, outfile)
print('Refresh Token: %s' % response['refresh_token'])
print('Access Token: %s' % response['access_token'])
print('Access Token Expiration Seconds: %s' % response['expires_in'])
Expand Down