PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/apiclient/sample_tools.py

https://code.google.com/p/google-api-python-client/
Python | 93 lines | 61 code | 8 blank | 24 comment | 3 complexity | 71bf2e426547f5bda6e4851933831761 MD5 | raw file
Possible License(s): Apache-2.0
  1. # Copyright (C) 2013 Google Inc.
  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. """Utilities for making samples.
  15. Consolidates a lot of code commonly repeated in sample applications.
  16. """
  17. __author__ = 'jcgregorio@google.com (Joe Gregorio)'
  18. __all__ = ['init']
  19. import argparse
  20. import httplib2
  21. import os
  22. from apiclient import discovery
  23. from oauth2client import client
  24. from oauth2client import file
  25. from oauth2client import tools
  26. def init(argv, name, version, doc, filename, scope=None, parents=[]):
  27. """A common initialization routine for samples.
  28. Many of the sample applications do the same initialization, which has now
  29. been consolidated into this function. This function uses common idioms found
  30. in almost all the samples, i.e. for an API with name 'apiname', the
  31. credentials are stored in a file named apiname.dat, and the
  32. client_secrets.json file is stored in the same directory as the application
  33. main file.
  34. Args:
  35. argv: list of string, the command-line parameters of the application.
  36. name: string, name of the API.
  37. version: string, version of the API.
  38. doc: string, description of the application. Usually set to __doc__.
  39. file: string, filename of the application. Usually set to __file__.
  40. parents: list of argparse.ArgumentParser, additional command-line flags.
  41. scope: string, The OAuth scope used.
  42. Returns:
  43. A tuple of (service, flags), where service is the service object and flags
  44. is the parsed command-line flags.
  45. """
  46. if scope is None:
  47. scope = 'https://www.googleapis.com/auth/' + name
  48. # Parser command-line arguments.
  49. parent_parsers = [tools.argparser]
  50. parent_parsers.extend(parents)
  51. parser = argparse.ArgumentParser(
  52. description=doc,
  53. formatter_class=argparse.RawDescriptionHelpFormatter,
  54. parents=parent_parsers)
  55. flags = parser.parse_args(argv[1:])
  56. # Name of a file containing the OAuth 2.0 information for this
  57. # application, including client_id and client_secret, which are found
  58. # on the API Access tab on the Google APIs
  59. # Console <http://code.google.com/apis/console>.
  60. client_secrets = os.path.join(os.path.dirname(filename),
  61. 'client_secrets.json')
  62. # Set up a Flow object to be used if we need to authenticate.
  63. flow = client.flow_from_clientsecrets(client_secrets,
  64. scope=scope,
  65. message=tools.message_if_missing(client_secrets))
  66. # Prepare credentials, and authorize HTTP object with them.
  67. # If the credentials don't exist or are invalid run through the native client
  68. # flow. The Storage object will ensure that if successful the good
  69. # credentials will get written back to a file.
  70. storage = file.Storage(name + '.dat')
  71. credentials = storage.get()
  72. if credentials is None or credentials.invalid:
  73. credentials = tools.run_flow(flow, storage, flags)
  74. http = credentials.authorize(http = httplib2.Http())
  75. # Construct a service object via the discovery service.
  76. service = discovery.build(name, version, http=http)
  77. return (service, flags)