Skip to content

Commit 355557a

Browse files
committed
New Maps-Engine sample.
Reviewed in https://codereview.appspot.com/36120043/. Index: samples/maps_engine/README =================================================================== new file mode 100644
1 parent d8cc458 commit 355557a

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

‎samples/maps_engine/README‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This sample code demonstrates use of Google Maps Engine. These samples do not
2+
make use of the Discovery Service, but do use the the httplib2 library to make
3+
authenticated requests. For more information on Google Maps Engine, see
4+
developers.google.com/maps-engine/documentation/
5+
6+
api: mapsengine
7+
keywords: maps engine mapsengine
8+
author: Josh Livni

‎samples/maps_engine/maps_engine.py‎

279 Bytes
Binary file not shown.

‎samples/maps_engine/polygons.prj‎

116 Bytes
Binary file not shown.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Copyright 2013 Google Inc. All Rights Reserved.
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+
"""Utilities for making samples.
16+
17+
Consolidates a lot of code commonly repeated in sample applications.
18+
19+
This modified version returns http instead of a built service. Originally
20+
https://google-api-python-client.googlecode.com/hg/apiclient/sample_tools.py
21+
"""
22+
23+
__author__ = '[email protected] (Joe Gregorio)'
24+
__all__ = ['init']
25+
26+
27+
import argparse
28+
import os
29+
30+
import httplib2
31+
from oauth2client import client
32+
from oauth2client import file as oauth2client_file
33+
from oauth2client import tools
34+
35+
36+
def init(argv, name, version, doc, filename, scope=None, parents=[]):
37+
"""A common initialization routine for samples.
38+
39+
Many of the sample applications do the same initialization, which has now
40+
been consolidated into this function. This function uses common idioms found
41+
in almost all the samples, i.e. for an API with name 'apiname', the
42+
credentials are stored in a file named apiname.dat, and the
43+
client_secrets.json file is stored in the same directory as the application
44+
main file.
45+
46+
Args:
47+
argv: list of string, the command-line parameters of the application.
48+
name: string, name of the API.
49+
version: string, version of the API.
50+
doc: string, description of the application. Usually set to __doc__.
51+
filename: string, filename of the application. Usually set to __file__.
52+
scope: string, The OAuth scope used.
53+
parents: list of argparse.ArgumentParser, additional command-line flags.
54+
55+
Returns:
56+
A tuple of (http, flags), where http is the authenticated http object and
57+
flags is the parsed command-line flags.
58+
"""
59+
if scope is None:
60+
scope = 'https://www.googleapis.com/auth/' + name
61+
62+
# Parser command-line arguments.
63+
parent_parsers = [tools.argparser]
64+
parent_parsers.extend(parents)
65+
parser = argparse.ArgumentParser(
66+
description=doc,
67+
formatter_class=argparse.RawDescriptionHelpFormatter,
68+
parents=parent_parsers)
69+
flags = parser.parse_args(argv[1:])
70+
71+
# Name of a file containing the OAuth 2.0 information for this
72+
# application, including client_id and client_secret, which are found
73+
# on the API Access tab on the Google APIs
74+
# Console <http://code.google.com/apis/console>.
75+
client_secrets = os.path.join(os.path.dirname(filename),
76+
'client_secrets.json')
77+
78+
# Set up a Flow object to be used if we need to authenticate.
79+
flow = client.flow_from_clientsecrets(
80+
client_secrets,
81+
scope=scope,
82+
message=tools.message_if_missing(client_secrets))
83+
84+
# Prepare credentials, and authorize HTTP object with them.
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 = oauth2client_file.Storage(name + '.dat')
89+
credentials = storage.get()
90+
if credentials is None or credentials.invalid:
91+
credentials = tools.run_flow(flow, storage, flags)
92+
http = credentials.authorize(http=httplib2.Http())
93+
94+
# GME does not publish a discovery document, so we return the auth'd http
95+
# instead of building a service using the discovery module.
96+
return (http, flags)

0 commit comments

Comments
 (0)