blob: ae864666a5ec477814895c233641c26f2eb62dc6 [file] [log] [blame]
Jon Wayne Parrottcf672c22016-10-26 09:41:00 -07001# 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
17These credentials are needed to run the system test for OAuth2 credentials.
18It's expected that a developer will run this program manually once to obtain
19a refresh token. It's highly recommended to use a Google account created
Jon Wayne Parrotte40a9b32016-10-26 10:42:38 -070020specifically for testing.
Jon Wayne Parrottcf672c22016-10-26 09:41:00 -070021"""
22
23import json
24import os
25
26from oauth2client import client
27from oauth2client import tools
28
29HERE = os.path.dirname(__file__)
30CLIENT_SECRETS_PATH = os.path.abspath(os.path.join(
31 HERE, '..', 'system_tests', 'data', 'client_secret.json'))
32AUTHORIZED_USER_PATH = os.path.abspath(os.path.join(
33 HERE, '..', 'system_tests', 'data', 'authorized_user.json'))
34SCOPES = ['email', 'profile']
35
36
37class 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
44def 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
64if __name__ == '__main__':
65 main()