blob: 846d17f96265064070606c363ede3a364d543b5f [file] [log] [blame]
Joe Gregorio652898b2011-05-02 21:07:43 -04001#!/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"""$title
19
20$description
21
22Usage:
23 $$ python $name.py
24
25You can also get help on all the command-line flags the program understands
26by running:
27
28 $$ python $name.py --help
29
30To get detailed log output run:
31
32 $$ python $name.py --logging_level=DEBUG
33"""
34
35__author__ = 'jcgregorio@google.com (Joe Gregorio)'
36
37import gflags
38import httplib2
39import logging
40import pprint
41import sys
42
43from apiclient.discovery import build
44from oauth2client.file import Storage
Joe Gregorio7d791212011-05-16 21:58:52 -070045from oauth2client.client import AccessTokenRefreshError
Joe Gregorio652898b2011-05-02 21:07:43 -040046from oauth2client.client import OAuth2WebServerFlow
47from oauth2client.tools import run
48
49FLAGS = gflags.FLAGS
50
51# Set up a Flow object to be used if we need to authenticate. This
52# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
53# the information it needs to authenticate. Note that it is called
54# the Web Server Flow, but it can also handle the flow for native
55# applications <http://code.google.com/apis/accounts/docs/OAuth2.html#IA>
56# The client_id client_secret are copied from the API Access tab on
57# the Google APIs Console <http://code.google.com/apis/console>. When
58# creating credentials for this application be sure to choose an Application
59# type of "Installed application".
60FLOW = OAuth2WebServerFlow(
61 client_id='433807057907.apps.googleusercontent.com',
62 client_secret='jigtZpMApkRxncxikFpR+SFg',
63 scope='$scope',
64 user_agent='$name-cmdline-sample/1.0')
65
66# The gflags module makes defining command-line options easy for
67# applications. Run this program with the '--help' argument to see
68# all the flags that it understands.
69gflags.DEFINE_enum('logging_level', 'ERROR',
70 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
71 'Set the level of logging detail.')
72
73
74def main(argv):
75 # Let the gflags module process the command-line arguments
76 try:
77 argv = FLAGS(argv)
78 except gflags.FlagsError, e:
79 print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
80 sys.exit(1)
81
82 # Set the logging according to the command-line flag
83 logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
84
85 # If the Credentials don't exist or are invalid run through the native client
86 # flow. The Storage object will ensure that if successful the good
87 # Credentials will get written back to a file.
88 storage = Storage('$name.dat')
89 credentials = storage.get()
90 if credentials is None or credentials.invalid:
91 credentials = run(FLOW, storage)
92
93 # Create an httplib2.Http object to handle our HTTP requests and authorize it
94 # with our good Credentials.
95 http = httplib2.Http()
96 http = credentials.authorize(http)
97
98 service = build("$name", "$version", http=http)
99
Joe Gregorio7d791212011-05-16 21:58:52 -0700100 try:
101
Joe Gregorio652898b2011-05-02 21:07:43 -0400102$content
103
Joe Gregorio7d791212011-05-16 21:58:52 -0700104 except AccessTokenRefreshError:
105 print ("The credentials have been revoked or expired, please re-run"
106 "the application to re-authorize")
Joe Gregorio652898b2011-05-02 21:07:43 -0400107
108if __name__ == '__main__':
109 main(sys.argv)