Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(40)

Side by Side Diff: samples/oauth2cmdline/get_refresh_token.py

Issue 5362041: Command line program to get OAuth 2.0 refresh tokens.
Patch Set: Created 2 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python2.4
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (C) 2010 Google Inc.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 """Simple command-line sample for getting an OAuth 2.0 Refresh Token.
19
20 Usage:
21 $ python get_refresh_token.py
22
23 You can also get help on all the command-line flags the program understands
24 by running:
25
26 $ python get_refresh_token.py --help
27
28 """
29
30 __author__ = 'jcgregorio@google.com (Joe Gregorio)'
31
32 import gflags
33 import httplib2
34 import logging
35 import os
36 import pprint
37 import sys
38
39 from apiclient.discovery import build
40 from oauth2client.file import Storage
41 from oauth2client.client import Flow
42 from oauth2client.client import OAuth2WebServerFlow
43 from oauth2client.tools import run
44
45
46 FLAGS = gflags.FLAGS
47
48
49 gflags.DEFINE_spaceseplist('scopes',
50 'https://www.googleapis.com/auth/userinfo.email',
51 'A space separated list of scopes.')
52
53 gflags.DEFINE_string('client_id', None, 'The client id.')
54 gflags.MarkFlagAsRequired('client_id')
55
56 gflags.DEFINE_string('client_secret', None, 'The client secret.')
57 gflags.MarkFlagAsRequired('client_secret')
58
59 gflags.DEFINE_boolean('force_refresh', False, 'Force the flow to be run again.')
60
61 gflags.DEFINE_enum('logging_level', 'ERROR',
62 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
63 'Set the level of logging detail.')
64
65
66 def main(argv):
67 # Let the gflags module process the command-line arguments
68 try:
69 argv = FLAGS(argv)
70 except gflags.FlagsError, e:
71 print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
72 sys.exit(1)
73
74 # Set the logging according to the command-line flag
75 logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
76
77 if FLAGS.force_refresh:
78 try:
79 os.unlink('cmd-line.dat')
80 except OSError:
81 pass
82
83 FLOW = OAuth2WebServerFlow(
84 client_id=FLAGS.client_id,
85 client_secret=FLAGS.client_secret,
86 scope=FLAGS.scopes,
87 user_agent='get-refresh-token/1.0',
88 approval_prompt='force')
89
90 storage = Storage('cmd-line.dat')
91 credentials = storage.get()
92 if credentials is None or credentials.invalid:
93 credentials = run(FLOW, storage)
94
95 print 'The refresh token is:\n'
96 print credentials.refresh_token
97 print '\nThe refresh token has also been stored in the cmd-line.dat file.'
98
99
100 if __name__ == '__main__':
101 main(sys.argv)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld 1278:e6ce13d99bf5