Skip to content

Instantly share code, notes, and snippets.

@jeffgodwyll
Created June 19, 2016 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffgodwyll/eecea074563e7143e4a651e1a09176da to your computer and use it in GitHub Desktop.
Save jeffgodwyll/eecea074563e7143e4a651e1a09176da to your computer and use it in GitHub Desktop.
To generate refresh token to use with Google API's for user data
#!/usr/bin/python
#
# Copyright 2014 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Generates refresh token for an 'Installed Application flow'.
Heavily appropriated from:
https://github.com/googleads/googleads-python-lib/blob/master/examples/adwords/authentication/generate_refresh_token.py
"""
import argparse
import sys
from oauth2client import client
DEFAULT_CLIENT_ID = '615266322580-qfq65ulrkolucrv9qm9el1h2u0hmfmp1.apps.googleusercontent.com'
DEFAULT_CLIENT_SECRET = 'PsUwwSQ4gOJC4OCr55o0ufoO'
# Fit API OAuth2 scope.
SCOPE = 'https://www.googleapis.com/auth/fitness.activity.read'
parser = argparse.ArgumentParser(description='Generates a refresh token with '
'the provided credentials.')
parser.add_argument('--client_id', default=DEFAULT_CLIENT_ID,
help='Client Id retrieved from the Developer\'s Console.')
parser.add_argument('--client_secret', default=DEFAULT_CLIENT_SECRET,
help='Client Secret retrieved from the Developer\'s '
'Console.')
parser.add_argument('--additional_scopes', default=None,
help='Additional scopes to apply when generating the '
'refresh token. Each scope should be separated by a comma.')
def main(client_id, client_secret, scopes):
"""Retrieve and display the access and refresh token."""
flow = client.OAuth2WebServerFlow(
client_id=client_id,
client_secret=client_secret,
scope=scopes,
redirect_uri='http://localhost:8080/oauth2callback')
authorize_url = flow.step1_get_authorize_url()
print ('Log into the Google Account you use to access your AdWords account'
'and go to the following URL: \n%s\n' % (authorize_url))
print 'After approving the token enter the verification code (if specified)'
code = raw_input('Code: ').strip()
try:
credential = flow.step2_exchange(code)
except client.FlowExchangeError, e:
print 'Authentication has failed: %s' % e
sys.exit(1)
else:
print ('OAuth2 authorization successful!\n\n'
'Your access token is:\n %s\n\nYour refresh token is:\n %s'
% (credential.access_token, credential.refresh_token))
if __name__ == '__main__':
args = parser.parse_args()
configured_scopes = [SCOPE]
if not (any([args.client_id, DEFAULT_CLIENT_ID]) and
any([args.client_secret, DEFAULT_CLIENT_SECRET])):
raise AttributeError('No client_id or client_secret specified.')
if args.additional_scopes:
configured_scopes.extend(
args.additional_scopes.replace(' ', '').split(','))
main(args.client_id, args.client_secret, configured_scopes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment