1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 import sys
34 import time
35 import webbrowser
36
37 from osgeo import gdal
38
39 SCOPES = {
40 'ft': 'https://www.googleapis.com/auth/fusiontables',
41 'storage': 'https://www.googleapis.com/auth/devstorage.read_only',
42 'storage-rw': 'https://www.googleapis.com/auth/devstorage.read_write'
43 }
44
45
46
47
48
49
51 print('')
52 print('Usage: gdal_auth_py [-s scope]')
53 print(' - interactive use.')
54 print('')
55 print('or:')
56 print('Usage: gdal_auth.py login [-s scope] ')
57 print('Usage: gdal_auth.py auth2refresh [-s scope] auth_token')
58 print('Usage: gdal_auth.py refresh2access [-s scope] refresh_token')
59 print('')
60 print('scopes: ft/storage/storage-rw/full_url')
61 print('')
62 sys.exit(1)
63
64
66 scope = SCOPES['ft']
67 token_in = None
68 command = None
69
70 argv = gdal.GeneralCmdLineProcessor(sys.argv)
71 if argv is None:
72 sys.exit(0)
73
74
75 i = 1
76 while i < len(argv):
77 arg = argv[i]
78
79 if arg == '-s' and i < len(argv) - 1:
80 if argv[i + 1] in SCOPES:
81 scope = SCOPES[argv[i + 1]]
82 elif argv[i + 1].startswith('http'):
83 scope = argv[i + 1]
84 else:
85 print('Scope %s not recognised.' % argv[i + 1])
86 Usage()
87 sys.exit(1)
88 i = i + 1
89
90 elif arg[0] == '-':
91 Usage()
92
93 elif command is None:
94 command = arg
95
96 elif token_in is None:
97 token_in = arg
98
99 else:
100 Usage()
101
102 i = i + 1
103
104 if command is None:
105 command = 'interactive'
106
107 if command == 'login':
108 print(gdal.GOA2GetAuthorizationURL(scope))
109 elif command == 'auth2refresh':
110 print(gdal.GOA2GetRefreshToken(token_in, scope))
111 elif command == 'refresh2access':
112 print(gdal.GOA2GetAccessToken(token_in, scope))
113 elif command != 'interactive':
114 Usage()
115 else:
116
117 print('Authorization requested for scope:')
118 print(scope)
119 print('')
120 print('Please login and authorize access in web browser...')
121
122 webbrowser.open(gdal.GOA2GetAuthorizationURL(scope))
123
124 time.sleep(2.0)
125
126 print('')
127 print('Enter authorization token:')
128 auth_token = sys.stdin.readline()
129
130 refresh_token = gdal.GOA2GetRefreshToken(auth_token, scope)
131
132 print('Refresh Token:' + refresh_token)
133 print('')
134 if scope == SCOPES['ft']:
135 print('Consider setting a configuration option like:')
136 print('GFT_REFRESH_TOKEN=' + refresh_token)
137 elif scope in (SCOPES['storage'], SCOPES['storage-rw']):
138 print('Consider setting a configuration option like:')
139 print('GS_OAUTH2_REFRESH_TOKEN=' + refresh_token)
140
141
142 if __name__ == '__main__':
143 sys.exit(main(sys.argv))
144