diff options
author | Jon Wayne Parrott <jonwayne@google.com> | 2016-10-26 09:41:00 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-26 09:41:00 -0700 |
commit | cf672c2c749d9a2c33408ec92d5d8d679dcf80d6 (patch) | |
tree | c5f621c9635e39022ea7f3d0868d780780194e7c /scripts | |
parent | f7b927905b797e0d58aaa06f8ff074ea95e980d0 (diff) | |
download | google-auth-library-python-cf672c2c749d9a2c33408ec92d5d8d679dcf80d6.tar.gz |
Add oauth2.credentials system tests (#56)
* Add script to generate user auth tokens.
* Add oauth2.credentials system tests
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/obtain_user_auth.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/scripts/obtain_user_auth.py b/scripts/obtain_user_auth.py new file mode 100644 index 0000000..fd2afd8 --- /dev/null +++ b/scripts/obtain_user_auth.py @@ -0,0 +1,65 @@ +# Copyright 2016 Google Inc. +# +# 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. + +"""This program obtains a set of user credentials. + +These credentials are needed to run the system test for OAuth2 credentials. +It's expected that a developer will run this program manually once to obtain +a refresh token. It's highly recommended to use a Google account created +specifically created for testing. +""" + +import json +import os + +from oauth2client import client +from oauth2client import tools + +HERE = os.path.dirname(__file__) +CLIENT_SECRETS_PATH = os.path.abspath(os.path.join( + HERE, '..', 'system_tests', 'data', 'client_secret.json')) +AUTHORIZED_USER_PATH = os.path.abspath(os.path.join( + HERE, '..', 'system_tests', 'data', 'authorized_user.json')) +SCOPES = ['email', 'profile'] + + +class NullStorage(client.Storage): + """Null storage implementation to prevent oauth2client from failing + on storage.put.""" + def locked_put(self, credentials): + pass + + +def main(): + flow = client.flow_from_clientsecrets(CLIENT_SECRETS_PATH, SCOPES) + + print('Starting credentials flow...') + credentials = tools.run_flow(flow, NullStorage()) + + # Save the credentials in the same format as the Cloud SDK's authorized + # user file. + data = { + 'type': 'authorized_user', + 'client_id': flow.client_id, + 'client_secret': flow.client_secret, + 'refresh_token': credentials.refresh_token + } + + with open(AUTHORIZED_USER_PATH, 'w') as fh: + json.dump(data, fh, indent=4) + + print('Created {}.'.format(AUTHORIZED_USER_PATH)) + +if __name__ == '__main__': + main() |