Created
September 30, 2014 12:16
-
-
Save qrtt1/7a9c25a8ba082450f131 to your computer and use it in GitHub Desktop.
產生 google compute engine OAuth 認證檔的小幫手
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import optparse | |
from oauth2client.client import flow_from_clientsecrets | |
from oauth2client.file import Storage | |
OAUTH_SCOPE = 'https://www.googleapis.com/auth/compute' | |
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob' | |
def gen_credentials_file(json_secret, credentials_file): | |
flow = flow_from_clientsecrets( | |
json_secret, | |
OAUTH_SCOPE, | |
REDIRECT_URI) | |
print 'Go to the following link in your browser: ' + flow.step1_get_authorize_url() | |
code = raw_input('Enter verification code: ').strip() | |
credentials = flow.step2_exchange(code) | |
storage = Storage(credentials_file) | |
storage.put(credentials) | |
print 'The credentials_file saved to %s' % credentials_file | |
def get_args(): | |
parser = optparse.OptionParser() | |
parser.add_option('-s', '--secret', action="store", dest="json_secret") | |
parser.add_option( | |
'-c', | |
'--credentials_file', | |
action="store", | |
dest="credentials_file") | |
options, remainder = parser.parse_args() | |
if not options.json_secret: | |
raise ValueError("no secret to load") | |
if not options.credentials_file: | |
raise ValueError("no credentials_file path to save") | |
return options | |
if __name__ == "__main__": | |
options = get_args() | |
gen_credentials_file(options.json_secret, options.credentials_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment