Skip to content

Commit

Permalink
Change the usage of generate_oauth2_credentials.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
nya3jp committed Jul 4, 2015
1 parent 7f1ab40 commit ba6535e
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions tools/generate_oauth2_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""Performs OAuth2 Web Server Flow to obtain credentials.
Usage:
generate_oauth2_credentials.py --client_id=CLIENT_ID --client_secret=CLIENT_SECRET --credentials_json=OUTPUT_JSON_PATH
generate_oauth2_credentials.py [--client_id=CLIENT_ID --client_secret=CLIENT_SECRET] OUTPUT_JSON_PATH
"""

import os
Expand All @@ -29,20 +29,28 @@

FLAGS = gflags.FLAGS

gflags.DEFINE_string('client_id', None, '')
gflags.DEFINE_string('client_secret', None, '')
gflags.DEFINE_string('credentials_json', None, '')
TEST_CLIENT_ID = '958069810280-th697if59r9scrf1qh0sg6gd9d9u0kts.apps.googleusercontent.com'
TEST_CLIENT_SECRET = '5nlcvd54WycOd8h8w7HD0avT'

gflags.DEFINE_string('client_id', TEST_CLIENT_ID, '')
gflags.DEFINE_string('client_secret', TEST_CLIENT_SECRET, '')
gflags.MarkFlagAsRequired('client_id')
gflags.MarkFlagAsRequired('client_secret')
gflags.MarkFlagAsRequired('credentials_json')


def main(unused_argv):
def main(argv):
if len(argv) != 2:
print 'usage: generate_oauth2_credentials.py OUTPUT_JSON_PATH'
return 1
output_json_path = argv[1]

flow = oauth2client.client.OAuth2WebServerFlow(
client_id=FLAGS.client_id,
client_secret=FLAGS.client_secret,
scope=hyou.client.GOOGLE_SPREADSHEET_SCOPES)
url = flow.step1_get_authorize_url('urn:ietf:wg:oauth:2.0:oob')

print
print 'Please visit this URL to get the authorization code:'
print url
print
Expand All @@ -51,11 +59,14 @@ def main(unused_argv):

credentials = flow.step2_exchange(code)

with open(FLAGS.credentials_json, 'w') as f:
with open(output_json_path, 'w') as f:
os.fchmod(f.fileno(), 0600)
f.write(credentials.to_json())

print 'Credentials successfully saved to %s' % FLAGS.credentials_json
print
print 'Credentials successfully saved to %s' % output_json_path
print
print 'WARNING: Keep it in a safe location! With the credentials, all your Google Drive documents can be accessed.'


if __name__ == '__main__':
Expand Down

0 comments on commit ba6535e

Please sign in to comment.