Jon Wayne Parrott | cf672c2 | 2016-10-26 09:41:00 -0700 | [diff] [blame] | 1 | # Copyright 2016 Google Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """This program obtains a set of user credentials. |
| 16 | |
| 17 | These credentials are needed to run the system test for OAuth2 credentials. |
| 18 | It's expected that a developer will run this program manually once to obtain |
| 19 | a refresh token. It's highly recommended to use a Google account created |
Jon Wayne Parrott | e40a9b3 | 2016-10-26 10:42:38 -0700 | [diff] [blame] | 20 | specifically for testing. |
Jon Wayne Parrott | cf672c2 | 2016-10-26 09:41:00 -0700 | [diff] [blame] | 21 | """ |
| 22 | |
| 23 | import json |
| 24 | import os |
| 25 | |
| 26 | from oauth2client import client |
| 27 | from oauth2client import tools |
| 28 | |
| 29 | HERE = os.path.dirname(__file__) |
| 30 | CLIENT_SECRETS_PATH = os.path.abspath(os.path.join( |
| 31 | HERE, '..', 'system_tests', 'data', 'client_secret.json')) |
| 32 | AUTHORIZED_USER_PATH = os.path.abspath(os.path.join( |
| 33 | HERE, '..', 'system_tests', 'data', 'authorized_user.json')) |
| 34 | SCOPES = ['email', 'profile'] |
| 35 | |
| 36 | |
| 37 | class NullStorage(client.Storage): |
| 38 | """Null storage implementation to prevent oauth2client from failing |
| 39 | on storage.put.""" |
| 40 | def locked_put(self, credentials): |
| 41 | pass |
| 42 | |
| 43 | |
| 44 | def main(): |
| 45 | flow = client.flow_from_clientsecrets(CLIENT_SECRETS_PATH, SCOPES) |
| 46 | |
| 47 | print('Starting credentials flow...') |
| 48 | credentials = tools.run_flow(flow, NullStorage()) |
| 49 | |
| 50 | # Save the credentials in the same format as the Cloud SDK's authorized |
| 51 | # user file. |
| 52 | data = { |
| 53 | 'type': 'authorized_user', |
| 54 | 'client_id': flow.client_id, |
| 55 | 'client_secret': flow.client_secret, |
| 56 | 'refresh_token': credentials.refresh_token |
| 57 | } |
| 58 | |
| 59 | with open(AUTHORIZED_USER_PATH, 'w') as fh: |
| 60 | json.dump(data, fh, indent=4) |
| 61 | |
| 62 | print('Created {}.'.format(AUTHORIZED_USER_PATH)) |
| 63 | |
| 64 | if __name__ == '__main__': |
| 65 | main() |