blob: e10a744de3dfb832ba74cb386f8b82cf3719fa7b [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
45from oauth2client.client import OAuth2WebServerFlow
46from oauth2client.tools import run
47
48FLAGS = gflags.FLAGS
49
50# Set up a Flow object to be used if we need to authenticate. This
51# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
52# the information it needs to authenticate. Note that it is called
53# the Web Server Flow, but it can also handle the flow for native
54# applications <http://code.google.com/apis/accounts/docs/OAuth2.html#IA>
55# The client_id client_secret are copied from the API Access tab on
56# the Google APIs Console <http://code.google.com/apis/console>. When
57# creating credentials for this application be sure to choose an Application
58# type of "Installed application".
59FLOW = OAuth2WebServerFlow(
60 client_id='433807057907.apps.googleusercontent.com',
61 client_secret='jigtZpMApkRxncxikFpR+SFg',
62 scope='$scope',
63 user_agent='$name-cmdline-sample/1.0')
64
65# The gflags module makes defining command-line options easy for
66# applications. Run this program with the '--help' argument to see
67# all the flags that it understands.
68gflags.DEFINE_enum('logging_level', 'ERROR',
69 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
70 'Set the level of logging detail.')
71
72
73def main(argv):
74 # Let the gflags module process the command-line arguments
75 try:
76 argv = FLAGS(argv)
77 except gflags.FlagsError, e:
78 print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
79 sys.exit(1)
80
81 # Set the logging according to the command-line flag
82 logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
83
84 # If the Credentials don't exist or are invalid run through the native client
85 # flow. The Storage object will ensure that if successful the good
86 # Credentials will get written back to a file.
87 storage = Storage('$name.dat')
88 credentials = storage.get()
89 if credentials is None or credentials.invalid:
90 credentials = run(FLOW, storage)
91
92 # Create an httplib2.Http object to handle our HTTP requests and authorize it
93 # with our good Credentials.
94 http = httplib2.Http()
95 http = credentials.authorize(http)
96
97 service = build("$name", "$version", http=http)
98
99$content
100
101
102if __name__ == '__main__':
103 main(sys.argv)