Python源码示例:googleapiclient.discovery.build_from_document()
示例1
def get_conn(self):
"""
Retrieves the connection to Cloud Firestore.
:return: Google Cloud Firestore services object.
"""
if not self._conn:
http_authorized = self._authorize()
# We cannot use an Authorized Client to retrieve discovery document due to an error in the API.
# When the authorized customer will send a request to the address below
# https://www.googleapis.com/discovery/v1/apis/firestore/v1/rest
# then it will get the message below:
# > Request contains an invalid argument.
# At the same time, the Non-Authorized Client has no problems.
non_authorized_conn = build("firestore", self.api_version, cache_discovery=False)
self._conn = build_from_document(
non_authorized_conn._rootDesc, # pylint: disable=protected-access
http=http_authorized
)
return self._conn
示例2
def get_service(api='gmail', version='v1', auth='service', scopes=None, uri_file=None):
global DISCOVERY_CACHE
key = api + version + auth + str(threading.current_thread().ident)
if key not in DISCOVERY_CACHE:
credentials = get_credentials(auth)
if uri_file:
uri_file = uri_file.strip()
if uri_file.startswith('{'):
DISCOVERY_CACHE[key] = discovery.build_from_document(uri_file, credentials=credentials)
else:
with open(uri_file, 'r') as cache_file:
DISCOVERY_CACHE[key] = discovery.build_from_document(cache_file.read(), credentials=credentials)
else:
DISCOVERY_CACHE[key] = discovery.build(api, version, credentials=credentials)
return DISCOVERY_CACHE[key]
示例3
def _build_service_from_document(credentials, document_path):
"""Builds an API client from a local discovery document
Args:
credentials (OAuth2Credentials): Credentials that will be used to
authenticate the API calls.
document_path (str): The local path of the discovery document
Returns:
object: A Resource object with methods for interacting with the service.
"""
with open(document_path, 'r') as f:
discovery_data = json.load(f)
return discovery.build_from_document(
service=discovery_data,
credentials=credentials
)
# pylint: disable=too-many-instance-attributes
示例4
def get_authenticated_service(args, credentials_folder):
client_secrets_file = credentials_folder + "client_secrets.json"
youtube_read_write_ssl_scope = "https://www.googleapis.com/auth/youtube.force-ssl"
youtube_api_service_name = "youtube"
youtube_api_version = "v3"
missing_clients_secrets_message = """
WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file
found at:
%s
with information from the APIs Console
https://developers.google.com/console
For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
client_secrets_file))
flow = flow_from_clientsecrets(client_secrets_file,
scope=youtube_read_write_ssl_scope,
message=missing_clients_secrets_message)
storage = Storage(credentials_folder + "reveal-stored-oauth2.json")
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, args)
# Trusted testers can download this discovery document from the developers page
# and it should be in the same directory with the code.
with open(credentials_folder + "youtube-v3-discoverydocument.json", "r") as f:
doc = f.read()
return build_from_document(doc, credentials=credentials)