def GetService(version=DEFAULT_VERSION):
  """Builds the adexchangebuyer service used for the REST API."""
  http = httplib2.Http()

  credentials = client.SignedJwtAssertionCredentials(
      SERVICE_ACCOUNT_EMAIL,
      open(KEY_FILE).read(),
      scope=SCOPE)

  http = credentials.authorize(http)

  if version in ADEXCHANGEBUYER_VERSIONS:
    # Initialize client for Ad Exchange Buyer API
    service = build('adexchangebuyer', version, http=http)
  elif version in ADEXCHANGEBUYERII_VERSIONS:
    discovery_url = ('https://%s/$discovery/rest?version=%s'
                     % (ADEXCHANGEBUYERII_SERVICE_NAME, version))
    discovery_doc = urllib2.urlopen(discovery_url).read()
    service = build_from_document(service=discovery_doc, http=http)
  else:
    raise ValueError('Invalid version provided. Supported versions are: %s'
                     % ', '.join(ADEXCHANGEBUYER_VERSIONS +
                                 ADEXCHANGEBUYERII_VERSIONS))

  return service
  def test_building_with_base_remembers_base(self):
    with open(datafile('plus.json')) as plusfile:
        discovery = plusfile.read()

    base = "https://www.example.com/"
    plus = build_from_document(discovery, base=base)
    self.assertEqual("https://www.googleapis.com/plus/v1/", plus._baseUrl)
예제 #3
0
파일: tuber.py 프로젝트: todokku/anonygram
    def get_authenticated_service(self, args):
        # This variable defines a message to display if the client_secrets_file is
        # missing.
        missing_client_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://console.developers.google.com
        
        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__), self.client_secrets_file))

        flow = flow_from_clientsecrets(self.client_secrets_file,
                                       scope=youtube_read_write_ssl_scope,
                                       message=missing_client_secrets_message)

        storage = Storage("%s-oauth2.json" % 'tuber.py')
        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
        # https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest
        with open("youtube-v3-discoverydocument.json", "r") as f:
            doc = f.read()
            return build_from_document(doc,
                                       http=credentials.authorize(
                                           httplib2.Http()))
예제 #4
0
    def test_building_with_base_remembers_base(self):
        with open(datafile("plus.json")) as plusfile:
            discovery = plusfile.read()

        base = "https://www.example.com/"
        plus = build_from_document(discovery, base=base)
        self.assertEqual("https://www.googleapis.com/plus/v1/", plus._baseUrl)
예제 #5
0
 def test_can_build_from_local_deserialized_document(self):
     with open(datafile("plus.json")) as plusfile:
         discovery = plusfile.read()
     discovery = simplejson.loads(discovery)
     plus = build_from_document(discovery, base="https://www.googleapis.com/")
     self.assertTrue(plus is not None)
     self.assertTrue(hasattr(plus, "activities"))
예제 #6
0
    def init(self, **kwargs):
        """Sets up the API service from test_googleplus_discovery.

    Pass a requestBuilder or http kwarg to inject expected HTTP requests and
    responses.
    """
        oauth_googleplus.json_service = discovery.build_from_document(DISCOVERY_DOC, **kwargs)
def CreateApi(api_version):
  """Creates mock API for a given Google Compute Engine API version.

  Args:
    api_version: Version of the API demanded. For example: 'v1beta16'.

  Returns:
    Tuple (mock, api). mock is an instance of MockServer which can be used to
    program responses for specific requests, api is an instance of Google
    Compute Engine API (google-api-python-client).
  """
  discovery_path = os.path.join(
      os.path.dirname(__file__),
      'compute',
      '{version}.json'.format(version=api_version))

  with open(discovery_path) as discovery_file:
    discovery_document = discovery_file.read()

  discovery_json = json.loads(discovery_document)

  parser = mock_api_parser.Parser(discovery_json)
  methods = parser.Parse()

  mock = mock_api_server.MockServer(methods)
  api = discovery.build_from_document(
      discovery_document, requestBuilder=mock.BuildRequest)
  return mock, gce_api.ComputeApi(api,
                                  version.get(discovery_json.get('version')),
                                  gce_api.GetSetOfApiMethods(discovery_json))
예제 #8
0
 def test_can_build_from_local_deserialized_document(self):
     discovery = open(datafile('plus.json')).read()
     discovery = simplejson.loads(discovery)
     plus = build_from_document(discovery,
                                base="https://www.googleapis.com/")
     self.assertTrue(plus is not None)
     self.assertTrue(hasattr(plus, 'activities'))
예제 #9
0
def CreateApi(api_version):
  """Creates mock API for a given Google Compute Engine API version.

  Args:
    api_version: Version of the API demanded. For example: 'v1'.

  Returns:
    Tuple (mock, api). mock is an instance of MockServer which can be used to
    program responses for specific requests, api is an instance of Google
    Compute Engine API (google-api-python-client).
  """
  discovery_path = os.path.join(
      os.path.dirname(__file__),
      'compute',
      '{version}.json'.format(version=api_version))

  with open(discovery_path) as discovery_file:
    discovery_document = discovery_file.read()

  discovery_json = json.loads(discovery_document)

  parser = mock_api_parser.Parser(discovery_json)
  methods = parser.Parse()

  mock = mock_api_server.MockServer(methods)
  api = discovery.build_from_document(
      discovery_document, requestBuilder=mock.BuildRequest)
  return mock, gce_api.ComputeApi(api,
                                  version.get(discovery_json.get('version')),
                                  gce_api.GetSetOfApiMethods(discovery_json))
예제 #10
0
    def __init__(self, auth):
        self.auth = auth
        http = auth.authorize(
            httplib2.Http(cache="/tmp/.pygsheets_cache", timeout=10))
        data_path = os.path.join(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
            "data")
        with open(os.path.join(data_path, "sheets_discovery.json")) as jd:
            self.service = discovery.build_from_document(jload(jd), http=http)
        with open(os.path.join(data_path, "drive_discovery.json")) as jd:
            self.driveService = discovery.build_from_document(jload(jd),
                                                              http=http)
        self._spreadsheeets = []
        self.batch_requests = dict()
        self.retries = 1

        self._fetch_sheets()
예제 #11
0
  def init(self, **kwargs):
    """Sets up the API service from test_googleplus_discovery.

    Pass a requestBuilder or http kwarg to inject expected HTTP requests and
    responses.
    """
    oauth_googleplus.json_service = discovery.build_from_document(
      DISCOVERY_DOC, **kwargs)
def main():
    # Load the local copy of the discovery document. discovery.json file is available to download
    # from the samples documentation page:
    # https://developers.google.com/cloud-devices/v1/samples/samples
    f = file(os.path.join(os.path.dirname(__file__), "discovery.json"), "r")
    discovery = f.read()
    f.close()

    # Get OAuth 2.0 credentials and authorize.
    http = httplib2.Http()
    credentials = get_credentials()
    http = credentials.authorize(http)

    # Construct an API client interface from the local documents
    api_client = build_from_document(
        discovery, base="https://www.googleapis.com/weave.app/v1/", http=http)

    print "Created a API Client\n"
    #response = api_client.commands().list().execute()
    #print response

    # Listing devices, request to devices.list API method, returns a list of devices
    # available to user. More details about the method:
    # https://developers.google.com/cloud-devices/v1/reference/cloud-api/devices/list
    print "Here " + str(type(api_client))
    object = api_client.devices()
    print "There " + str(type(object))
    print api_client.devices().list()
    response = api_client.devices().list().execute()

    print json.dumps(response, indent=2)

    devices = response.get('devices', [])
    if not devices:
        print 'No devices, creating one.'

        device = create_device(api_client)
    else:
        device = devices[0]
    print 'Available device: %s' % device['id']

    print 'Sending a new command to the device'
    command = {
        'name': 'storage.list',  # Command name to execute.
        'parameters': {
            'path': '/tmp'  # Required command parameters
        },
        'deviceId': device['id']  # Device to send the command to.
    }
    # Calling commands.insert method to send command to the device, more details about the method:
    # https://developers.google.com/cloud-devices/v1/reference/cloud-api/commands/insert
    command = api_client.commands().insert(body=command).execute()

    # The state of the command will be "queued". In normal situation a client may request
    # command again via commands.get API method to get command execution results, but our fake
    # device does not actually receive any commands, so it will never be executed.
    print 'Sent command to the device:\n%s' % json.dumps(command, indent=2)
예제 #13
0
  def _InitializeService(self):
    """Creates a new API service for interacting with BigQuery."""
    document = None

    with open(DISCOVERY_FILE, 'rb') as f:
      document = f.read()
      f.close()

    self.service = build_from_document(document, http=self._http)
예제 #14
0
def build_service(credentials):
    http = httplib2.Http()
    http = credentials.authorize(http)

    discovery_file = open('glass.v1.rest.json')
    service = build_from_document(discovery_file.read(), http=http)
    discovery_file.close()

    return service
예제 #15
0
파일: bqlib.py 프로젝트: addsict/bqlib
    def build_apiclient(discovery_document, http):
        from apiclient import discovery
        from bigquery_client import BigqueryModel, BigqueryHttp

        bigquery_model = BigqueryModel()
        return discovery.build_from_document(
            discovery_document, http=http,
            model=bigquery_model,
            requestBuilder=BigqueryHttp.Factory(bigquery_model))
예제 #16
0
def get_authenticated_service():
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
                                   scope=YOUTUBE_READ_WRITE_SSL_SCOPE,
                                   message=MISSING_CLIENT_SECRETS_MESSAGE)
    storage = Storage("config/%s-oauth2.json" % argv[0])
    credentials = storage.get()
    with open("config/youtube-v3-discoverydocument.json", "r") as f:
        doc = f.read()
        return build_from_document(doc,
                                   http=credentials.authorize(httplib2.Http()))
예제 #17
0
    def build_apiclient(discovery_document, http):
        from apiclient import discovery
        from bigquery_client import BigqueryModel, BigqueryHttp

        bigquery_model = BigqueryModel()
        return discovery.build_from_document(
            discovery_document,
            http=http,
            model=bigquery_model,
            requestBuilder=BigqueryHttp.Factory(bigquery_model))
예제 #18
0
    def service(self):
        """
        Establishes, caches and returns either a new or cached instance of a
        Google apiclient resource object, pertinent to a particular Google
        API; in our case, the Drive API.
        """
        if self._service is not None:
            yield self._service
            return

        storage = self._get_credential_storage()
        if storage is not None:
            credentials = storage.get()
        else:
            credentials = None

        if credentials is None:
            credentials = self._obtain_credentials()

        debug("Authenticating")
        import httplib2

        #if debug.enabled(): httplib2.debuglevel = 4

        http = credentials.authorize(
            httplib2.Http(cache = self._get_config_dir("http_cache"))
        )

        debug("Loading Google Drive service from config")

        from apiclient.discovery import build_from_document, DISCOVERY_URI

        debug("Downloading API service")

        import uritemplate
        url = uritemplate.expand(DISCOVERY_URI, {
            'api': 'drive',
            'apiVersion': 'v2'
        })
        res, content = http.request(url)

        apistr = None
        if res.status in [ 200, 202 ]:
            # API expires every minute.
            apistr = content

        if not apistr:
            raise NoServiceError

        debug("Building Google Drive service from document")
        self._service = build_from_document(
            apistr, http = http, base = DISCOVERY_URI
        )

        yield self._service
예제 #19
0
파일: __init__.py 프로젝트: quadrater/gsync
    def service(self):
        """
        Establishes, caches and returns either a new or cached instance of a
        Google apiclient resource object, pertinent to a particular Google
        API; in our case, the Drive API.
        """
        if self._service is not None:
            yield self._service
            return

        storage = self._get_credential_storage()
        if storage is not None:
            credentials = storage.get()
        else:
            credentials = None

        if credentials is None:
            credentials = self._obtain_credentials()

        debug("Authenticating")
        import httplib2

        #if debug.enabled(): httplib2.debuglevel = 4

        http = credentials.authorize(
            httplib2.Http(cache = self._get_config_dir("http_cache"))
        )

        debug("Loading Google Drive service from config")

        from apiclient.discovery import build_from_document, DISCOVERY_URI
        
        debug("Downloading API service")

        import uritemplate
        url = uritemplate.expand(DISCOVERY_URI, {
            'api': 'drive',
            'apiVersion': 'v2'
        })
        res, content = http.request(url)

        apistr = None
        if res.status in [ 200, 202 ]:
            # API expires every minute.
            apistr = content

        if not apistr:
            raise NoServiceError

        debug("Building Google Drive service from document")
        self._service = build_from_document(
            apistr, http = http, base = DISCOVERY_URI
        )

        yield self._service
def create_pubsub_client(disdoc, http=None):
    """Create an authenticated pubsub client.

    See https://cloud.google.com/pubsub/configure
    """
    credentials = oauth2client.GoogleCredentials.get_application_default()
    if credentials.create_scoped_required():
        credentials = credentials.create_scoped(PUBSUB_SCOPES)
    if not http:
        http = httplib2.Http(timeout=45)
    credentials.authorize(http)
    return discovery.build_from_document(disdoc, http=http)
def create_pubsub_client(disdoc, http=None):
    """Create an authenticated pubsub client.

    See https://cloud.google.com/pubsub/configure
    """
    credentials = oauth2client.GoogleCredentials.get_application_default()
    if credentials.create_scoped_required():
        credentials = credentials.create_scoped(PUBSUB_SCOPES)
    if not http:
        http = httplib2.Http(timeout=45)
    credentials.authorize(http)
    return discovery.build_from_document(disdoc, http=http)
def CreateService(discovery_doc, creds):
  """Create a Google API service.

  Args:
    discovery_doc: Discovery doc used to configure service.
    creds: Credentials used to authorize service.
  Returns:
    Authorized Google API service.
  """
  http = httplib2.Http()
  creds.authorize(http)
  return build_from_document(discovery_doc, APIS_BASE, http=http)
예제 #23
0
def get_authenticated_service(args):
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SSL_SCOPE,
                                   message="Invalid client secrets file.")
    storage = Storage("%s-oauth2.json" % sys.argv[0])
    credentials = storage.get()

    if credentials is None or credentials.invalid:
        credentials = run_flow(flow, storage, args)

    with open(DEVELOPER_SECRETS_FILE, "r") as f:
        doc = f.read()
    return build_from_document(doc, http=credentials.authorize(httplib2.Http()))
예제 #24
0
def get_authenticated_service(args):
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SSL_SCOPE, message=MISSING_CLIENT_SECRETS_MESSAGE)

    storage = Storage("main.py-oauth2.json")
    credentials = storage.get()

    if credentials is None or credentials.invalid:
        credentials = run_flow(flow, storage, args)

    with open("youtube-v3-discoverydocument.json", "r") as f:
        doc = f.read()
        return build_from_document(doc, http=credentials.authorize(httplib2.Http()))
예제 #25
0
def CreateService(discovery_doc, creds):
    """Create a Google API service.

  Args:
    discovery_doc: Discovery doc used to configure service.
    creds: Credentials used to authorize service.
  Returns:
    Authorized Google API service.
  """
    http = httplib2.Http()
    creds.authorize(http)
    return build_from_document(discovery_doc, APIS_BASE, http=http)
예제 #26
0
def authenticate(service,
                 discovery,
                 version,
                 sub=None,
                 access_token=None,
                 http=None):
    """
    Authenticate to the Gmail API with the account specified by 'sub'.

    :param service: The API service object to authentication.
    :param discovery: The service descriptor file to use for API endpoint discovery.
    :param version: The version of the Gmail API to authentication to.
    :param sub: (optional) The user to authenticate the service object to.
    :param access_token: (optional) The OAuth2 token (usually from the cache) to use for authentication.
    :param http: Optional HTTP object to use (used in tests).
    :return: A tuple of (the authenticated credential object, the authenticated http object,
             and the service object).
    """

    if access_token:
        credentials = AccessTokenCredentials(access_token,
                                             user_agent="mailbeaker/1.0")
    else:
        logging.info("No access_token provided, establishing new token.",
                     extra={"email": sub})

        key = settings.GOOGLE_OAUTH2_PRIVATE_KEY
        credentials = \
            SignedJwtAssertionCredentials(service_account_name=settings.GOOGLE_OAUTH2_SERVICE_ACCOUNT_EMAIL,
                                          private_key=key,
                                          scope=settings.GOOGLE_OAUTH2_SCOPE,
                                          sub=sub)

    if not http:
        # Authorize the httplib2.Http object with our credentials
        http = httplib2.Http()
        http = credentials.authorize(http)

    # We'll read the description of this API's function from
    # a JSON input file. Normally the client reaches out to either:
    # - https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest
    # - https://www.googleapis.com/discovery/v1/apis/admin/directory_v1/rest
    # ... to get this information via apiclient.discovery.build(),
    # but that method causes a significant perfomance impact.
    path = os.path.dirname(os.path.realpath(__file__))
    path = os.path.join(path, discovery)
    discovery = open(path).read()
    service = build_from_document(discovery,
                                  http=http,
                                  base="https://www.googleapis.com/")

    return credentials, http, service
예제 #27
0
 def get_youtube_service(self):
     flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_PARTNER_SCOPE,
     message='need client_secrets.json')
 
     storage = Storage("%s-oauth2.json" % sys.argv[0])
     credentials = storage.get()
 
     if credentials is None or credentials.invalid:
         credentials = run_flow(flow, storage, args)
 
     with open("youtube-v3-api-captions.json", "r") as f:
         doc = f.read()
         return build_from_document(doc, http=credentials.authorize(httplib2.Http()))
예제 #28
0
 def get_authenticated_service(self, args):
     flow = flow_from_clientsecrets(
         self.__CLIENT_SECRETS_FILE,
         scope=self.__YOUTUBE_READ_WRITE_SSL_SCOPE,
         message=self.__MISSING_CLIENT_SECRETS_MESSAGE)
     storage = Storage(self.__CLIENT_OAUTH2_ACCESS_TOKEN_FILE)
     credentials = storage.get()
     if credentials is None or credentials.invalid:
         credentials = run_flow(flow, storage, args)
     with open(self.__CLIENT_API_CAPTIONS_FILE, "r", encoding="utf-8") as f:
         doc = f.read()
         return build_from_document(doc,
                                    http=credentials.authorize(
                                        httplib2.Http()))
예제 #29
0
파일: __init__.py 프로젝트: feyrune/gsync
    def service(self):
        if self._service is not None:
            return self._service

        storage = self._getCredentialStorage()
        if storage is not None:
            credentials = storage.get()
        else:
            credentials = None

        if credentials is None:
            credentials = self._obtainCredentials()

        debug("Authenticating")
        import httplib2

        if debug.enabled():
            httplib2.debuglevel = 4

        http = credentials.authorize(
            httplib2.Http(cache = self._getConfigDir("http_cache"))
        )

        debug("Loading Google Drive service from config")

        from apiclient.discovery import build_from_document, DISCOVERY_URI
        
        debug("Downloading API service")

        import uritemplate
        url = uritemplate.expand(DISCOVERY_URI, {
            'api': 'drive',
            'apiVersion': 'v2'
        })
        res, content = http.request(url)

        apistr = None
        if res.status in [ 200, 202 ]:
            # API expires every minute.
            apistr = str(content)

        if not apistr:
            return None

        debug("Building Google Drive service from document")
        self._service = build_from_document(
            apistr, http = http, base = DISCOVERY_URI
        )

        return self._service
예제 #30
0
  def build(cls, serviceName, version, **kwargs):
    discoveryServiceUrl = kwargs.pop('discoveryServiceUrl', DISCOVERY_URI)
    key = ndb.Key(cls, serviceName, cls, version, cls, discoveryServiceUrl)
    discovery_doc = key.get()

    if discovery_doc is None or discovery_doc.expired:
        # Note that we DO NOT pass the incoming http object here, we use the
        # default so that we retrieve the doc without any user authentication.
        document = retrieve_discovery_doc(
            serviceName, version, discoveryServiceUrl=discoveryServiceUrl)
        discovery_doc = cls(key=key, document=document)
        discovery_doc.put()

    return build_from_document(discovery_doc.document, **kwargs)
예제 #31
0
    def build(cls, serviceName, version, **kwargs):
        discoveryServiceUrl = kwargs.pop('discoveryServiceUrl', DISCOVERY_URI)
        key = ndb.Key(cls, serviceName, cls, version, cls, discoveryServiceUrl)
        discovery_doc = key.get()

        if discovery_doc is None or discovery_doc.expired:
            # Note that we DO NOT pass the incoming http object here, we use the
            # default so that we retrieve the doc without any user authentication.
            document = retrieve_discovery_doc(
                serviceName, version, discoveryServiceUrl=discoveryServiceUrl)
            discovery_doc = cls(key=key, document=document)
            discovery_doc.put()

        return build_from_document(discovery_doc.document, **kwargs)
예제 #32
0
def get_authenticated_service(args):
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
                                   scope=YOUTUBE_READ_WRITE_SSL_SCOPE,
                                   message=MISSING_CLIENT_SECRETS_MESSAGE)
    storage = Storage("%s-oauth2.json" % sys.argv[0])
    credentials = storage.get()

    if credentials is None or credentials.invalid:
        return False

    with open("youtube-v3-api-captions.json", "r") as f:
        doc = f.read()
        return build_from_document(doc,
                                   http=credentials.authorize(httplib2.Http()))
예제 #33
0
파일: __init__.py 프로젝트: omriiluz/gsync
    def service(self):
        if self._service is not None:
            return self._service

        storage = self._getCredentialStorage()
        if storage is not None:
            credentials = storage.get()
        else:
            credentials = None

        if credentials is None:
            credentials = self._obtainCredentials()

        debug("Authenticating")
        import httplib2

        #if debug.enabled(): httplib2.debuglevel = 4

        http = credentials.authorize(
            httplib2.Http(cache = self._getConfigDir("http_cache"))
        )

        debug("Loading Google Drive service from config")

        from apiclient.discovery import build_from_document, DISCOVERY_URI
        
        debug("Downloading API service")

        import uritemplate
        url = uritemplate.expand(DISCOVERY_URI, {
            'api': 'drive',
            'apiVersion': 'v2'
        })
        res, content = http.request(url)

        apistr = None
        if res.status in [ 200, 202 ]:
            # API expires every minute.
            apistr = content

        if not apistr:
            return None

        debug("Building Google Drive service from document")
        self._service = build_from_document(
            apistr, http = http, base = DISCOVERY_URI
        )

        return self._service
    def build(cls, service_name, version, **kwargs):
        """Builds the client object."""
        discovery_service_url = kwargs.pop('discovery_service_url',
                                           DISCOVERY_URI)
        discovery_doc = DiscoveryDoc.read_from_cache(service_name, version,
                                                     discovery_service_url)
        if discovery_doc is None or discovery_doc.expired:
            # If None, retrieve_discovery_doc() will use default
            http = kwargs.get('http')
            discovery_doc = retrieve_discovery_doc(
                service_name, version, http=http,
                discovery_service_url=discovery_service_url)
            discovery_doc.save()

        return build_from_document(discovery_doc.document, **kwargs)
예제 #35
0
def get_authenticated_service(args):
  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SSL_SCOPE,
    message=MISSING_CLIENT_SECRETS_MESSAGE)

  storage = Storage("%s-oauth2.json" % sys.argv[0])
  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("youtube-v3-discoverydocument.json", "r") as f:
    doc = f.read()
    return build_from_document(doc, http=credentials.authorize(httplib2.Http()))
예제 #36
0
파일: domain.py 프로젝트: djpetti/hd-domain
  def __init__(self, domain):
    # Make it wait a little longer before aborting fetches.
    urlfetch.set_default_fetch_deadline(30)

    self.domain = domain

    self.__authorize_http_instance()
    discovery_doc = self.__get_discovery_doc("admin", "directory_v1",
                                             http=self.authorized_http)
    self.service = build_from_document(discovery_doc,
                                       http=self.authorized_http)

    self.users = self.service.users()
    self.groups = self.service.groups()
    self.members = self.service.members()
예제 #37
0
def get_authenticated_service(args):
  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SSL_SCOPE,
    message=MISSING_CLIENT_SECRETS_MESSAGE)

  storage = Storage("%s-oauth2.json" % sys.argv[0])
  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("youtube-v3-discoverydocument.json", "r") as f:
    doc = f.read()
    return build_from_document(doc, http=credentials.authorize(httplib2.Http()))
예제 #38
0
  def test_search_for_links(self):
    # should only search for urls without paths
    for urls in [], [], ['http://a/b'], ['https://c/d/e', 'https://f.com/g']:
      self.gp.domain_urls = urls
      self.assertEqual([], self.gp.search_for_links())

    # TODO: actually check search query. (still haven't figured out how with
    # RequestMockBuilder etc. :/ see granary/test/test_googleplus.py for more.)
    self.gp.domain_urls = ['http://a', 'https://b/', 'http://c/d/e']
    oauth_googleplus.json_service = discovery.build_from_document(
      gr_test_googleplus.DISCOVERY_DOC, requestBuilder=http.RequestMockBuilder({
        'plus.activities.search':
          (None, json.dumps({'items': [gr_test_googleplus.ACTIVITY_GP]})),
      }))
    self.assertEqual([gr_test_googleplus.ACTIVITY_AS], self.gp.search_for_links())
    def __enter__(self):
        http = HttpMockSequence(self._responses)
        native_request_method = http.request

        # Collecting requests to validate at __exit__.
        def _request_wrapper(*args, **kwargs):
            self._actual_requests.append(args + (kwargs.get('body', ''),))
            return native_request_method(*args, **kwargs)

        http.request = _request_wrapper
        discovery = requests.get(
            'https://www.googleapis.com/discovery/v1/apis/ml/v1/rest')
        service_mock = build_from_document(discovery.json(), http=http)
        with mock.patch.object(
                hook.MLEngineHook, 'get_conn', return_value=service_mock):
            return hook.MLEngineHook()
예제 #40
0
def build(serviceName, version, creds):
    key = ndb.Key(DiscoveryDocument, serviceName, DiscoveryDocument, version,
                  DiscoveryDocument, DISCOVERY_URI)
    discovery_doc = key.get()

    if discovery_doc is None or discovery_doc.expired:
        # If None, RetrieveDiscoveryDoc() will use default
        document = _RetrieveDiscoveryDoc(serviceName, version)
        discovery_doc = DiscoveryDocument(key=key, document=document)
        discovery_doc.put()

    http = httplib2.Http()
    if creds:
        # Authorize the Http instance with the passed credentials
        creds.authorize(http)
    return build_from_document(discovery_doc.document, http=http)
예제 #41
0
def get_service():
    """Generates the service object from the discovery document.

    The discovery document is expected to be in the tool's source
    location, named SitePublishApi.discovery.

    Returns:
      The service instance.
    """
    # TODO: make discovery doc path overrideable at the command line

    discovery_doc_fname = os.path.join(os.path.dirname(__file__),
                                       'SitePublishApi.discovery')
    discovery_doc = open(discovery_doc_fname).read()
    site_publish_service = discovery.build_from_document(discovery_doc)
    return site_publish_service
예제 #42
0
def get_service():
    """Generates the service object from the discovery document.

    The discovery document is expected to be in the tool's source
    location, named SitePublishApi.discovery.

    Returns:
      The service instance.
    """
    # TODO: make discovery doc path overrideable at the command line

    discovery_doc_fname = os.path.join(
        os.path.dirname(__file__),
        'SitePublishApi.discovery')
    discovery_doc = open(discovery_doc_fname).read()
    site_publish_service = discovery.build_from_document(discovery_doc)
    return site_publish_service
예제 #43
0
def document_api_from_discovery_document(uri):
  """Document the given API.

  Args:
    uri: string, URI of discovery document.
  """
  http = httplib2.Http()
  response, content = http.request(FLAGS.discovery_uri)
  discovery = simplejson.loads(content)

  service = build_from_document(discovery)

  name = discovery['version']
  version = safe_version(discovery['version'])

  document_collection_recursive(
      service, '%s_%s.' % (name, version), discovery, discovery)
    def __enter__(self):
        http = HttpMockSequence(self._responses)
        native_request_method = http.request

        # Collecting requests to validate at __exit__.
        def _request_wrapper(*args, **kwargs):
            self._actual_requests.append(args + (kwargs.get('body', ''), ))
            return native_request_method(*args, **kwargs)

        http.request = _request_wrapper
        discovery = requests.get(
            'https://www.googleapis.com/discovery/v1/apis/ml/v1/rest')
        service_mock = build_from_document(discovery.json(), http=http)
        with mock.patch.object(hook.MLEngineHook,
                               'get_conn',
                               return_value=service_mock):
            return hook.MLEngineHook()
예제 #45
0
def document_api_from_discovery_document(uri):
    """Document the given API.

  Args:
    uri: string, URI of discovery document.
  """
    http = httplib2.Http()
    response, content = http.request(FLAGS.discovery_uri)
    discovery = simplejson.loads(content)

    service = build_from_document(discovery)

    name = discovery['version']
    version = safe_version(discovery['version'])

    document_collection_recursive(service, '%s_%s.' % (name, version),
                                  discovery, discovery)
예제 #46
0
def build(serviceName,version,creds):
    key = ndb.Key(DiscoveryDocument, serviceName, 
                  DiscoveryDocument, version, 
                  DiscoveryDocument, DISCOVERY_URI)
    discovery_doc = key.get()

    if discovery_doc is None or discovery_doc.expired:
      # If None, RetrieveDiscoveryDoc() will use default
      document = _RetrieveDiscoveryDoc(serviceName, version)
      discovery_doc = DiscoveryDocument(key=key, document=document)
      discovery_doc.put()

    http = httplib2.Http()
    if creds:
        # Authorize the Http instance with the passed credentials
        creds.authorize(http)
    return build_from_document(discovery_doc.document, http=http)
    def build(cls, service_name, version, **kwargs):
        """Builds the client object."""
        discovery_service_url = kwargs.pop('discovery_service_url',
                                           DISCOVERY_URI)
        key = ndb.Key(cls, service_name, cls, version, cls,
                      discovery_service_url)
        discovery_doc = key.get()

        if discovery_doc is None or discovery_doc.expired:
            # If None, retrieve_discovery_doc() will use default
            http = kwargs.get('http')
            document = retrieve_discovery_doc(
                service_name, version, http=http,
                discovery_service_url=discovery_service_url)
            discovery_doc = cls(key=key, document=document)
            discovery_doc.put()

        return build_from_document(discovery_doc.document, **kwargs)
예제 #48
0
def get_authenticated_service(_args):
    """
    Authorize the request and store the authorization credentials.
    """
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
                                   scope=YOUTUBE_RW_SSL_SCOPE,
                                   message=MISSING_CLIENT_SECRETS_MESSAGE)

    storage = Storage(OAUTH_CREDENTIALS)
    credentials = storage.get()

    if credentials is None or credentials.invalid:
        credentials = run_flow(flow, storage, _args)

    with open(DISCOVERY_DOCUMENT, "r") as f:
        doc = f.read()
        return build_from_document(doc,
                                   http=credentials.authorize(httplib2.Http()))
예제 #49
0
    def get_authenticated_service(args):

        CLIENT_SECRETS_FILE = "client_secret.json"

        # This OAuth 2.0 access scope allows for full read/write access to the
        # authenticated user's account and requires requests to use an SSL connection.
        YOUTUBE_READ_WRITE_SSL_SCOPE = "https://www.googleapis.com/auth/youtube.force-ssl"
        #YOUTUBE_API_SERVICE_NAME = "youtube"
        #YOUTUBE_API_VERSION = "v3"

        # This variable defines a message to display if the CLIENT_SECRETS_FILE is
        # missing.
        MISSING_CLIENT_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://console.developers.google.com
        
        For more information about the client_secrets.json file format, please visit:
        https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
        """

        flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
                                       scope=YOUTUBE_READ_WRITE_SSL_SCOPE,
                                       message=MISSING_CLIENT_SECRETS_MESSAGE)

        storage = Storage("%s-oauth2.json" % sys.argv[0])
        credentials = storage.get()

        if credentials is None or credentials.invalid:
            credentials = run_flow(flow, storage, args)

    #return credentials

    # Trusted testers can download this discovery document from the developers page
    # and it should be in the same directory with the code.
        with open("something.json", "r") as f:
            doc = f.read()

        return build_from_document(doc,
                                   http=credentials.authorize(httplib2.Http()))
예제 #50
0
def authenticate(service, discovery, version, sub=None, access_token=None, http=None):
    """
    Authenticate to the Gmail API with the account specified by 'sub'.

    :param service: The API service object to authentication.
    :param discovery: The service descriptor file to use for API endpoint discovery.
    :param version: The version of the Gmail API to authentication to.
    :param sub: (optional) The user to authenticate the service object to.
    :param access_token: (optional) The OAuth2 token (usually from the cache) to use for authentication.
    :param http: Optional HTTP object to use (used in tests).
    :return: A tuple of (the authenticated credential object, the authenticated http object,
             and the service object).
    """

    if access_token:
        credentials = AccessTokenCredentials(access_token, user_agent="mailbeaker/1.0")
    else:
        logging.info("No access_token provided, establishing new token.",
                     extra={"email": sub})

        key = settings.GOOGLE_OAUTH2_PRIVATE_KEY
        credentials = \
            SignedJwtAssertionCredentials(service_account_name=settings.GOOGLE_OAUTH2_SERVICE_ACCOUNT_EMAIL,
                                          private_key=key,
                                          scope=settings.GOOGLE_OAUTH2_SCOPE,
                                          sub=sub)

    if not http:
        # Authorize the httplib2.Http object with our credentials
        http = httplib2.Http()
        http = credentials.authorize(http)

    # We'll read the description of this API's function from
    # a JSON input file. Normally the client reaches out to either:
    # - https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest
    # - https://www.googleapis.com/discovery/v1/apis/admin/directory_v1/rest
    # ... to get this information via apiclient.discovery.build(),
    # but that method causes a significant perfomance impact.
    path = os.path.dirname(os.path.realpath(__file__))
    path = os.path.join(path, discovery)
    discovery = open(path).read()
    service = build_from_document(discovery, http=http, base="https://www.googleapis.com/")

    return credentials, http, service
예제 #51
0
    def __init__(self, ioloop, state, command_wrapper, delegate):
        self.state = state
        self.http = httplib2.Http()
        if not os.path.isfile(_API_CLIENT_FILE):
            credentials = {
                'oauth_client_id': '',
                'oauth_secret': '',
                'api_key': ''
            }
            credentials_f = open(_API_CLIENT_FILE + '.samlpe', 'w')
            credentials_f.write(
                json.dumps(credentials,
                           sort_keys=True,
                           indent=2,
                           separators=(',', ': ')))
            credentials_f.close()
            raise Exception('Missing ' + _API_CLIENT_FILE)

        credentials_f = open(_API_CLIENT_FILE)
        credentials = json.load(credentials_f)
        credentials_f.close()

        self.oauth_client_id = credentials['oauth_client_id']
        self.oauth_secret = credentials['oauth_secret']
        self.api_key = credentials['api_key']

        if not os.path.isfile(_API_DISCOVERY_FILE):
            raise Exception('Download https://developers.google.com/'
                            'cloud-devices/v1/discovery.json')

        f = open(_API_DISCOVERY_FILE)
        discovery = f.read()
        f.close()
        self.gcd = build_from_document(discovery,
                                       developerKey=self.api_key,
                                       http=self.http)

        self.ioloop = ioloop
        self.active = True
        self.device_id = None
        self.credentials = None
        self.delegate = delegate
        self.command_handler = command_wrapper(ioloop)
예제 #52
0
def writemoment():
  if request.method == 'POST' and 'credentials' in session:
    credentials = session['credentials']

    http = httplib2.Http()
    http = credentials.authorize(http)

    discoveryDoc = open('./discovery.json', 'r').read()

    service = build_from_document(
      discoveryDoc, 'https://www.googleapis.com/plus/v1moments/', http=http)

    moments = service.moments()
    body = json.loads(request.form['activity-json'])

    req = moments.insert(userId='me', collection='vault', body=body)
    resp = req.execute()

    session['message'] = "Moment rendered!"
  return redirect(url_for('index'))
예제 #53
0
def main():
  http = httplib2.Http()

  # Load the local copy of the discovery document
  f = file(os.path.join(os.path.dirname(__file__), "buzz.json"), "r")
  discovery = f.read()
  f.close()

  # Optionally load a futures discovery document
  f = file(os.path.join(os.path.dirname(__file__), "../../apiclient/contrib/buzz/future.json"), "r")
  future = f.read()
  f.close()

  # Construct a service from the local documents
  service = build_from_document(discovery,
      base="https://www.googleapis.com/",
      http=http,
      future=future)

  pprint.pprint(service.activities().search(q='lady gaga').execute())
  def LoadApi(self, http, api_server, api_version, args):
    """Load an API either from a discovery document or a local file.

    Args:
      http: An httplib2.Http object for making HTTP requests.
      api_server: The location of the http server.
      api_version: The api_version to load.
      args: The args object from the CLI.

    Returns:
      The newly created DNS API Service Object.

    Raises:
      ToolException: If an error occurs in loading the API service object.
    """
    # Load the discovery document.
    discovery_document = None

    # Try to download the discovery document
    url = '{server}/discovery/v1/apis/dns/{version}/rest'.format(
        server=api_server.rstrip('/'),
        version=api_version)
    response, content = http.request(url)

    if response.status == 200:
      discovery_document = content
    if discovery_document is None:
      raise c_exc.ToolException('Couldn\'t load discovery')

    try:
      discovery_document = json.loads(discovery_document)
    except ValueError:
      raise errors.InvalidJsonError()


    api = discovery.build_from_document(
        discovery_document,
        http=http,
        model=model.JsonModel())
    return WrapApiIfNeeded(api, args)
  def __init__(
      self, credentials, project_id=None, zone_name=None, settings=None):
    """Initializes the GceProject class.

    Sets default values for class attributes. See the instance resource for
    more information:

    https://developers.google.com/compute/docs/reference/v1beta14/instances

    Args:
      credentials: An oauth2client.client.Credentials object.
      project_id: A string name for the Compute Engine project.
      zone_name: The string name of the zone.
      settings: A dictionary of GCE settings. These settings will override
          any settings in the settings.json file. See the settings.json file for
          key names.
    """

    settings_file = os.path.join(
        os.path.dirname(__file__), '../../settings.json')
    self.settings = json.loads(open(settings_file, 'r').read())
    if settings:
      self.settings.update(settings)

    self.gce_url = '%s/%s' % (GCE_URL, self.settings['compute']['api_version'])

    discovery_doc_path = 'discovery/compute/%s.json' % self.settings['compute']['api_version']
    discovery_doc = open(discovery_doc_path, 'r').read()

    auth_http = self._auth_http(credentials)
    self.service = discovery.build_from_document(
        discovery_doc, self.settings['compute']['api_version'], http=auth_http)

    self.project_id = project_id
    if not self.project_id:
      self.project_id = self.settings['project']

    self.zone_name = zone_name
    if not self.zone_name:
      self.zone_name = self.settings['compute']['zone']
  def setUp(self):
    parser = mock_api_parser.Parser(SIMPLE_API)
    methods = parser.Parse()
    self._server = mock_api_server.MockServer(methods)
    self._api = discovery.build_from_document(
        SIMPLE_API, requestBuilder=self._server.BuildRequest)
    self._networks = self._api.networks()
    self._operations = self._api.operations()

    # Basic sanity checks.
    self.assertEquals(['Network', 'Operation'], sorted(parser._parsed_schemas))
    self.assertTrue(isinstance(parser._parsed_schemas['Network'],
                               mock_api_types.ObjectType))
    self.assertTrue(isinstance(parser._parsed_schemas['Operation'],
                               mock_api_types.ObjectType))
    self.assertEquals(
        ['compute.networks.insert',
         'compute.networks.update',
         'compute.operations.delete',
         'compute.operations.get',
         'compute.operations.list'],
        sorted(methods))
예제 #57
0
  def __init__(self, ioloop, state, command_wrapper, delegate):
    self.state = state
    self.http = httplib2.Http()
    if not os.path.isfile(_API_CLIENT_FILE):
      credentials = {
          'oauth_client_id': '',
          'oauth_secret': '',
          'api_key': ''
      }
      credentials_f = open(_API_CLIENT_FILE + '.samlpe', 'w')
      credentials_f.write(json.dumps(credentials, sort_keys=True,
                                     indent=2, separators=(',', ': ')))
      credentials_f.close()
      raise Exception('Missing ' + _API_CLIENT_FILE)

    credentials_f = open(_API_CLIENT_FILE)
    credentials = json.load(credentials_f)
    credentials_f.close()

    self.oauth_client_id = credentials['oauth_client_id']
    self.oauth_secret = credentials['oauth_secret']
    self.api_key = credentials['api_key']

    if not os.path.isfile(_API_DISCOVERY_FILE):
      raise Exception('Download https://developers.google.com/'
                      'cloud-devices/v1/discovery.json')

    f = open(_API_DISCOVERY_FILE)
    discovery = f.read()
    f.close()
    self.gcd = build_from_document(discovery, developerKey=self.api_key,
                                   http=self.http)

    self.ioloop = ioloop
    self.active = True
    self.device_id = None
    self.credentials = None
    self.delegate = delegate
    self.command_handler = command_wrapper(ioloop)