def authorized_http(credentials):
    """Returns an http client that is authorized with the given credentials.

    Args:
        credentials (Union[
            google.auth.credentials.Credentials,
            oauth2client.client.Credentials]): The credentials to use.

    Returns:
        Union[httplib2.Http, google_auth_httplib2.AuthorizedHttp]: An
            authorized http client.
    """
    from googleapiclient.http import build_http

    if HAS_GOOGLE_AUTH and isinstance(
            credentials, google.auth.credentials.Credentials):
        if google_auth_httplib2 is None:
            raise ValueError(
                'Credentials from google.auth specified, but '
                'google-api-python-client is unable to use these credentials '
                'unless google-auth-httplib2 is installed. Please install '
                'google-auth-httplib2.')
        return google_auth_httplib2.AuthorizedHttp(credentials,
                                                   http=build_http())
    else:
        return credentials.authorize(build_http())
Beispiel #2
0
def authorized_http(credentials):
    """Returns an http client that is authorized with the given credentials.

    Args:
        credentials (Union[
            google.auth.credentials.Credentials,
            oauth2client.client.Credentials]): The credentials to use.

    Returns:
        Union[httplib2.Http, google_auth_httplib2.AuthorizedHttp]: An
            authorized http client.
    """
    from googleapiclient.http import build_http

    if HAS_GOOGLE_AUTH and isinstance(
            credentials, google.auth.credentials.Credentials):
        if google_auth_httplib2 is None:
            raise ValueError(
                'Credentials from google.auth specified, but '
                'google-api-python-client is unable to use these credentials '
                'unless google-auth-httplib2 is installed. Please install '
                'google-auth-httplib2.')
        return google_auth_httplib2.AuthorizedHttp(credentials,
                                                   http=build_http())
    else:
        return credentials.authorize(build_http())
Beispiel #3
0
def authorized_http(credentials, default_http=None):
    """Returns an http client that is authorized with the given credentials.

    Args:
        credentials (Union[
            google.auth.credentials.Credentials,
            oauth2client.client.Credentials]): The credentials to use.
        default_http (Union[httplib2.Http, None]): A httplib2.Http object which
            will be used to make http requests.

    Returns:
        Union[httplib2.Http, google_auth_httplib2.AuthorizedHttp]: An
            authorized http client.
    """
    http = default_http
    if not http:
        from googleapiclient.http import build_http
        http = build_http()

    if HAS_GOOGLE_AUTH and isinstance(credentials,
                                      google.auth.credentials.Credentials):
        if google_auth_httplib2 is None:
            raise ValueError(
                'Credentials from google.auth specified, but '
                'google-api-python-client is unable to use these credentials '
                'unless google-auth-httplib2 is installed. Please install '
                'google-auth-httplib2.')
        return google_auth_httplib2.AuthorizedHttp(credentials, http=http)
    else:
        return credentials.authorize(http)
def document_api(name, version):
  """Document the given API.

  Args:
    name: string, Name of the API.
    version: string, Version of the API.
  """
  try:
    service = build(name, version)
  except UnknownApiNameOrVersion as e:
    print 'Warning: {} {} found but could not be built.'.format(name, version)
    return

  http = build_http()
  response, content = http.request(
      uritemplate.expand(
          FLAGS.discovery_uri_template, {
              'api': name,
              'apiVersion': version})
          )
  discovery = json.loads(content)

  version = safe_version(version)

  document_collection_recursive(
      service, '%s_%s.' % (name, version), discovery, discovery)
Beispiel #5
0
def document_api(name, version):
    """Document the given API.

  Args:
    name: string, Name of the API.
    version: string, Version of the API.
  """
    try:
        service = build(name, version)
    except UnknownApiNameOrVersion as e:
        print 'Warning: {} {} found but could not be built.'.format(
            name, version)
        return

    http = build_http()
    response, content = http.request(
        uritemplate.expand(FLAGS.discovery_uri_template, {
            'api': name,
            'apiVersion': version
        }))
    discovery = json.loads(content)

    version = safe_version(version)

    document_collection_recursive(service, '%s_%s.' % (name, version),
                                  discovery, discovery)
Beispiel #6
0
    def google_calendar_credentials(self, key):
        """Loads and refreshes Google Calendar API credentials."""

        # Look up the user from the key.
        user = self.user(key)
        if not user:
            return None

        # Load the credentials from storage.
        try:
            json = user.get('google_calendar_credentials')
        except KeyError:
            warning('Failed to load Google Calendar credentials.')
            return None

        # Use the valid credentials.
        credentials = OAuth2Credentials.from_json(json)
        if credentials and not credentials.invalid:
            return credentials

        # Handle invalidation and expiration.
        if credentials and credentials.access_token_expired:
            try:
                info('Refreshing Google Calendar credentials.')
                credentials.refresh(build_http())
                return credentials
            except HttpAccessTokenRefreshError as e:
                warning('Google Calendar refresh failed: %s' % e)

        # Credentials are missing or refresh failed.
        warning('Deleting Google Calendar credentials.')
        self.delete_google_calendar_credentials(key)
        return None
  def test_http_request_to_from_json(self):
    http = build_http()
    media_upload = MediaFileUpload(
        datafile('small.png'), chunksize=500, resumable=True)
    req = HttpRequest(
        http,
        _postproc_none,
        'http://example.com',
        method='POST',
        body='{}',
        headers={'content-type': 'multipart/related; boundary="---flubber"'},
        methodId='foo',
        resumable=media_upload)

    json = req.to_json()
    new_req = HttpRequest.from_json(json, http, _postproc_none)

    self.assertEqual({'content-type':
                       'multipart/related; boundary="---flubber"'},
                       new_req.headers)
    self.assertEqual('http://example.com', new_req.uri)
    self.assertEqual('{}', new_req.body)
    self.assertEqual(http, new_req.http)
    self.assertEqual(media_upload.to_json(), new_req.resumable.to_json())

    self.assertEqual(random.random, new_req._rand)
    self.assertEqual(time.sleep, new_req._sleep)
Beispiel #8
0
    def test_http_request_to_from_json(self):
        def _postproc(*kwargs):
            pass

        http = build_http()
        media_upload = MediaFileUpload(datafile('small.png'),
                                       chunksize=500,
                                       resumable=True)
        req = HttpRequest(http,
                          _postproc,
                          'http://example.com',
                          method='POST',
                          body='{}',
                          headers={
                              'content-type':
                              'multipart/related; boundary="---flubber"'
                          },
                          methodId='foo',
                          resumable=media_upload)

        json = req.to_json()
        new_req = HttpRequest.from_json(json, http, _postproc)

        self.assertEqual(
            {'content-type': 'multipart/related; boundary="---flubber"'},
            new_req.headers)
        self.assertEqual('http://example.com', new_req.uri)
        self.assertEqual('{}', new_req.body)
        self.assertEqual(http, new_req.http)
        self.assertEqual(media_upload.to_json(), new_req.resumable.to_json())

        self.assertEqual(random.random, new_req._rand)
        self.assertEqual(time.sleep, new_req._sleep)
 def from_credentials(cls, credentials: credentials.Credentials):
     http_client = http.set_user_agent(http.build_http(),
                                       'django-cloud-deploy')
     auth_http = google_auth_httplib2.AuthorizedHttp(credentials,
                                                     http=http_client)
     return cls(
         discovery.build('cloudresourcemanager', 'v1', http=auth_http))
Beispiel #10
0
def download_mail_block(*args, **kwargs):
    count = 0
    t0 = timer()

    mail_block = args[0]
    credentials = args[1]
    mail_id_list = list()

    try:
        service = build('gmail', 'v1', credentials)
    except HttpError as error:
        app.logger.error('build error {!r}'.format(error))
        return

    batch = service.new_batch_http_request()
    for mail in mail_block:
        batch.add(service.users().messages().get(userId='me',
                                                 id=mail['id'],
                                                 format='minimal'),
                  callback=download_mail_batch_cb)
        mail_id_list.append(mail['id'])

    batch.execute(http=build_http())

    elapsed = timer() - t0
    app.logger.info('mail block count {} elapsed time {:.2f}s'.format(
        count, elapsed))
    return
Beispiel #11
0
    def _event_counts(self, time, user):
        """Retrieves a daily count of events using the Google Calendar API."""

        # Create an authorized connection to the API.
        storage = GoogleCalendarStorage(user.id)
        credentials = storage.get()
        if not credentials:
            error('No valid Google Calendar credentials.')
            return Counter()
        authed_http = credentials.authorize(http=build_http())
        service = discovery.build(API_NAME, API_VERSION, http=authed_http,
                                  cache_discovery=False)

        # Process calendar events for each day of the current month.
        first_date = time.replace(day=1, hour=0, minute=0, second=0,
                                  microsecond=0)
        _, last_day = monthrange(time.year, time.month)
        last_date = first_date.replace(day=last_day)
        page_token = None
        event_counts = Counter()
        while True:
            # Request this month's events.
            request = service.events().list(calendarId=CALENDAR_ID,
                                            timeMin=first_date.isoformat(),
                                            timeMax=last_date.isoformat(),
                                            singleEvents=True,
                                            pageToken=page_token)
            try:
                response = request.execute()
            except HttpAccessTokenRefreshError as e:
                warning('Google Calendar request failed: %s' % e)
                return Counter()

            # Iterate over the events from the current page.
            for event in response['items']:
                try:
                    # Count regular events.
                    start = parse(event['start']['dateTime'])
                    end = parse(event['end']['dateTime'])
                    for day in self._days_range(start, end):
                        event_counts[day] += 1
                except KeyError:
                    pass

                try:
                    # Count all-day events.
                    start = datetime.strptime(event['start']['date'],
                                              '%Y-%m-%d')
                    end = datetime.strptime(event['end']['date'], '%Y-%m-%d')
                    for day in self._days_range(start, end):
                        event_counts[day] += 1
                except KeyError:
                    pass

            # Move to the next page or stop.
            page_token = response.get('nextPageToken')
            if not page_token:
                break

        return event_counts
Beispiel #12
0
def build_fit_service():
    filename = "json/"
    scope = [
        "https://www.googleapis.com/auth/fitness.activity.read",
        "https://www.googleapis.com/auth/fitness.activity.write",
        "https://www.googleapis.com/auth/fitness.location.read",
        "https://www.googleapis.com/auth/fitness.location.write",
    ]
    # Name of a file containing the OAuth 2.0 information for this
    # application, including client_id and client_secret, which are found
    # on the API Access tab on the Google APIs
    # Console <http://code.google.com/apis/console>.
    client_secrets = os.path.join(os.path.dirname(filename),
                                  "client_secrets.json")

    # Set up a Flow object to be used if we need to authenticate.
    flow = client.flow_from_clientsecrets(
        client_secrets,
        scope=scope,
        message=tools.message_if_missing(client_secrets))

    # Prepare credentials, and authorize HTTP object with them.
    # If the credentials don't exist or are invalid run through the native client
    # flow. The Storage object will ensure that if successful the good
    # credentials will get written back to a file.
    storage = file.Storage(
        os.path.join(os.path.dirname(filename), "token.json"))
    credentials = storage.get()
    if credentials is None or credentials.invalid:
        # maybe add argv to support --noauth_local_webserver
        credentials = tools.run_flow(flow, storage)
    http = credentials.authorize(http=build_http())

    fit_service = build(serviceName='fitness', version='v1', http=http)
    return fit_service
Beispiel #13
0
    def _init_google_client(self):
        start_time = time.monotonic()
        delay = 2
        while True:
            http = build_http()
            if self.proxy_info:
                if self.proxy_info.get("type") == "socks5":
                    proxy_type = httplib2.socks.PROXY_TYPE_SOCKS5
                else:
                    proxy_type = httplib2.socks.PROXY_TYPE_HTTP

                http.proxy_info = httplib2.ProxyInfo(
                    proxy_type,
                    self.proxy_info["host"],
                    self.proxy_info["port"],
                    proxy_user=self.proxy_info.get("user"),
                    proxy_pass=self.proxy_info.get("pass"),
                )

            http = self.google_creds.authorize(http)

            try:
                # sometimes fails: httplib2.ServerNotFoundError: Unable to find the server at www.googleapis.com
                return build("storage", "v1", http=http)
            except (httplib2.ServerNotFoundError, socket.timeout):
                if time.monotonic() - start_time > 600:
                    raise

            # retry on DNS issues
            time.sleep(delay)
            delay = delay * 2
def document_api(name, version, uri):
    """Document the given API.

  Args:
    name: string, Name of the API.
    version: string, Version of the API.
    uri: string, URI of the API's discovery document
  """
    try:
        service = build(name, version)
    except UnknownApiNameOrVersion as e:
        print("Warning: {} {} found but could not be built.".format(
            name, version))
        return
    except HttpError as e:
        print("Warning: {} {} returned {}.".format(name, version, e))
        return

    http = build_http()
    response, content = http.request(
        uri or uritemplate.expand(FLAGS.discovery_uri_template, {
            "api": name,
            "apiVersion": version
        }))
    discovery = json.loads(content)

    version = safe_version(version)

    document_collection_recursive(service, "{}_{}.".format(name, version),
                                  discovery, discovery)
Beispiel #15
0
    def get_gcs_client(self) -> luigi.contrib.gcs.GCSClient:
        if (not os.path.isfile(self.discover_cache_local_path)):
            with open(self.discover_cache_local_path, "w") as f:
                try:
                    fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)

                    params = {"api": "storage", "apiVersion": "v1"}
                    discovery_http = build_http()
                    for discovery_url in (self._DISCOVERY_URI,
                                          self._V2_DISCOVERY_URI):
                        requested_url = uritemplate.expand(
                            discovery_url, params)
                        try:
                            content = _retrieve_discovery_doc(
                                requested_url, discovery_http, False)
                        except HttpError as e:
                            if e.resp.status == http_client.NOT_FOUND:
                                continue
                            else:
                                raise e
                        break
                    f.write(content)
                    fcntl.flock(f, fcntl.LOCK_UN)
                except IOError:
                    # try to read
                    pass

        with open(self.discover_cache_local_path, "r") as f:
            fcntl.flock(f, fcntl.LOCK_SH)
            descriptor = f.read()
            fcntl.flock(f, fcntl.LOCK_UN)
            return luigi.contrib.gcs.GCSClient(
                oauth_credentials=self._load_oauth_credentials(),
                descriptor=descriptor)
Beispiel #16
0
 def testTFXHttpRequest(self):
     req = telemetry_utils.TFXHttpRequest(
         http=http.build_http(),
         postproc=None,
         uri='http://example.com',
     )
     self.assertContainsInOrder(['tfx/', 'client_context:tfxpipeline;'],
                                req.headers['user-agent'])
Beispiel #17
0
def authorized_http(credentials):
    """Returns an http client that is authorized with the given credentials.

    Args:
        credentials (Union[
            google.auth.credentials.Credentials,
            oauth2client.client.Credentials]): The credentials to use.

    Returns:
        Union[httplib2.Http, google_auth_httplib2.AuthorizedHttp]: An
            authorized http client.
    """
    if HAS_GOOGLE_AUTH and isinstance(
            credentials, google.auth.credentials.Credentials):
        return google_auth_httplib2.AuthorizedHttp(credentials,
                                                   http=build_http())
    else:
        return credentials.authorize(build_http())
Beispiel #18
0
def authorized_http(credentials):
    """Returns an http client that is authorized with the given credentials.

    Args:
        credentials (Union[
            google.auth.credentials.Credentials,
            oauth2client.client.Credentials]): The credentials to use.

    Returns:
        Union[httplib2.Http, google_auth_httplib2.AuthorizedHttp]: An
            authorized http client.
    """
    if HAS_GOOGLE_AUTH and isinstance(
            credentials, google.auth.credentials.Credentials):
        return google_auth_httplib2.AuthorizedHttp(credentials,
                                                   http=build_http())
    else:
        return credentials.authorize(build_http())
Beispiel #19
0
 def _authorize(self) -> google_auth_httplib2.AuthorizedHttp:
     """
     Returns an authorized HTTP object to be used to build a Google cloud
     service hook connection.
     """
     credentials = self._get_credentials()
     http = build_http()
     http = set_user_agent(http, "airflow/" + version.version)
     authed_http = google_auth_httplib2.AuthorizedHttp(credentials, http=http)
     return authed_http
Beispiel #20
0
def document_api(name, version, uri, doc_destination_dir):
    """Document the given API.

    Args:
        name (str): Name of the API.
        version (str): Version of the API.
        uri (str): URI of the API's discovery document
        doc_destination_dir (str): relative path where the reference
            documentation should be saved.
  """
    http = build_http()
    resp, content = http.request(
        uri or uritemplate.expand(
            FLAGS.discovery_uri_template, {"api": name, "apiVersion": version}
        )
    )

    if resp.status == 200:
        discovery = json.loads(content)
        service = build_from_document(discovery)
        version = safe_version(version)
        doc_name = "{}.{}.json".format(name, version.replace("_", ""))

        discovery_file_path = DISCOVERY_DOC_DIR / doc_name
        revision = None

        pathlib.Path(discovery_file_path).touch(exist_ok=True)

        # Write discovery artifact to disk if revision equal or newer
        with open(discovery_file_path, "r+") as f:
            try:
                json_data = json.load(f)
                revision = json_data['revision']
            except json.JSONDecodeError:
                revision = None

            if revision is None or discovery['revision'] >= revision:
                # Reset position to the beginning
                f.seek(0)
                # Write the changes to disk
                json.dump(discovery, f, indent=2, sort_keys=True)
                # Truncate anything left as it's not needed
                f.truncate()

    elif resp.status == 404:
        print("Warning: {} {} not found. HTTP Code: {}".format(name, version, resp.status))
        return
    else:
        print("Warning: {} {} could not be built. HTTP Code: {}".format(name, version, resp.status))
        return

    document_collection_recursive(
        service, "{}_{}.".format(name, version), discovery, discovery, doc_destination_dir
    )
Beispiel #21
0
 def build_service(self, name, version):
     flow = client.flow_from_clientsecrets(self._client_secrets,
                                           scope=self._scope,
                                           message=tools.message_if_missing(
                                               self._client_secrets))
     storage = file.Storage(str(self.user_id) + '.dat')
     credentials = storage.get()
     if credentials is None or credentials.invalid:
         print('authorization_flow')
         credentials = self.authorization_flow(flow, storage)
     http = credentials.authorize(http=build_http())
     return discovery.build(name, version, http=http)
Beispiel #22
0
    def __init__(self, project_id, language, flags):
        self.flags = flags
        client_secrets = os.path.join(os.path.dirname(__file__),
                                      CLIENT_SECRETS_FILE)

        flow = client.flow_from_clientsecrets(
            client_secrets,
            SCOPES,
            message=tools.message_if_missing(client_secrets))

        storage = file.Storage(CREDENTIALS_STORAGE)
        credentials = storage.get()

        if credentials is None or credentials.invalid:
            credential_flags = argparse.Namespace(
                noauth_local_webserver=True,
                logging_level=logging.getLevelName(
                    logging.getLogger().getEffectiveLevel()))
            credentials = tools.run_flow(flow, storage, flags=credential_flags)

        http = credentials.authorize(http=build_http())

        self.gmb_services = {}
        for service_name in FEDERATED_SERVICES:
            with open(f"{service_name}{DISCOVERY_FILE_SUFFIX}"
                      ) as discovery_file:
                self.gmb_services[
                    service_name] = discovery.build_from_document(
                        discovery_file.read(),
                        base="https://www.googleapis.com/",
                        http=http)

        with open(GMB_DISCOVERY_FILE) as gmb_discovery_file:
            self.gmb_service = discovery.build_from_document(
                gmb_discovery_file.read(),
                base="https://www.googleapis.com/",
                http=http)

        self.project_id = project_id
        self.dataset_exists = False
        self.existing_tables = {}
        self.language = language

        with open(SCHEMAS_FILE) as schemas_file:
            self.schemas = json.load(schemas_file)

        self.bq_service = discovery.build("bigquery", "v2", http=http)
        self.nlp_service = discovery.build("language", "v1", http=http)

        if flags["topic_clustering"]:
            self.topic_clustering = TopicClustering()
Beispiel #23
0
def init(name, version, doc, filename, scope=None, discovery_filename=None):
	"""Extend the file of googleapisamples on Google's SDK libraries on github
	   Author: [email protected] (Joe Gregorio)
		"""

	from googleapiclient import discovery
	from googleapiclient.http import build_http
	import os	
	try:
		from oauth2client import client
		from oauth2client import file
		from oauth2client import tools
	except ImportError:
		raise ImportError('googleapiclient.sample_tools requires oauth2client. Please install oauth2client and try again.')

	if scope is None:
		scope = 'https://www.googleapis.com/auth/' + name

	  # Name of a file containing the OAuth 2.0 information for this
	  # application, including client_id and client_secret, which are found
	  # on the API Access tab on the Google APIs
	  # Console <http://code.google.com/apis/console>.
	client_secrets = os.path.join(os.path.dirname(filename),
							'client_secrets.json')

	  # Set up a Flow object to be used if we need to authenticate.
	flow = client.flow_from_clientsecrets(client_secrets,
										  scope=scope,
										  message=tools.message_if_missing(client_secrets))

	  # Prepare credentials, and authorize HTTP object with them.
	  # If the credentials don't exist or are invalid run through the native client
	  # flow. The Storage object will ensure that if successful the good
	  # credentials will get written back to a file.
	storage = file.Storage(name + '.dat')
	credentials = storage.get()
	if credentials is None or credentials.invalid:
		credentials = tools.run_flow(flow, storage)
	http = credentials.authorize(http=build_http())

	if discovery_filename is None:
	 # Construct a service object via the discovery service.
		service = discovery.build(name, version, http=http)
	else:
		# Construct a service object using a local discovery document file.
		with open(discovery_filename) as discovery_file:
			service = discovery.build_from_document(
					discovery_file.read(),
					base='https://www.googleapis.com/',
					http=http)
	return (service)
Beispiel #24
0
def oauth_step2(key, scope, code):
    """Exchanges and saves the OAuth credentials."""

    # Use scope-specific token exchange and storage steps.
    if scope == GOOGLE_CALENDAR_SCOPE:
        credentials = _google_calendar_step2(key, code)
        storage = GoogleCalendarStorage(key)
        credentials.set_store(storage)
        try:
            credentials.refresh(build_http())
        except HttpAccessTokenRefreshError as e:
            storage.delete()
            error('Token refresh error: %s' % e)
    else:
        error('Unknown OAuth scope: %s' % scope)
    def test_authorized_http(self):
        credentials = mock.Mock(spec=oauth2client.client.Credentials)

        # Overriding the default timeout value of the Http object.
        socket.setdefaulttimeout(42.0)
        http = build_http()

        authorized_http = _auth.authorized_http(credentials, http)

        http = credentials.authorize.call_args[0][0]

        self.assertEqual(authorized_http, credentials.authorize.return_value)
        self.assertIsInstance(http, httplib2.Http)
        self.assertIsInstance(http.timeout, float)
        # Check if the Http object's timeout is set to the override value.
        self.assertEqual(http.timeout, 42.0)
def document_api_from_discovery_document(uri):
    """Document the given API.

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

    service = build_from_document(discovery)

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

    document_collection_recursive(service, "{}_{}.".format(name, version),
                                  discovery, discovery)
def document_api_from_discovery_document(uri):
  """Document the given API.

  Args:
    uri: string, URI of discovery document.
  """
  http = build_http()
  response, content = http.request(FLAGS.discovery_uri)
  discovery = json.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)
Beispiel #28
0
def generate_all_api_documents(directory_uri=DIRECTORY_URI, doc_destination_dir=BASE):
    """Retrieve discovery artifacts and fetch reference documentations
    for all apis listed in the public discovery directory.
    args:
        directory_uri (str): uri of the public discovery directory.
        doc_destination_dir (str): relative path where the reference
            documentation should be saved.
    """
    api_directory = collections.defaultdict(list)
    http = build_http()
    resp, content = http.request(directory_uri)
    if resp.status == 200:
        directory = json.loads(content)["items"]
        for api in directory:
            document_api(
                api["name"],
                api["version"],
                api["discoveryRestUrl"],
                doc_destination_dir,
            )
            api_directory[api["name"]].append(api["version"])

        # sort by api name and version number
        for api in api_directory:
            api_directory[api] = sorted(api_directory[api])
        api_directory = collections.OrderedDict(
            sorted(api_directory.items(), key=lambda x: x[0])
        )

        markdown = []
        for api, versions in api_directory.items():
            markdown.append("## %s" % api)
            for version in versions:
                markdown.append(
                    "* [%s](http://googleapis.github.io/google-api-python-client/docs/dyn/%s_%s.html)"
                    % (version, api, safe_version(version))
                )
            markdown.append("\n")

        with open(BASE / "index.md", "w") as f:
            markdown = "\n".join(markdown)
            f.write(markdown)

    else:
        sys.exit("Failed to load the discovery document.")
Beispiel #29
0
    def __init__(self):
        self.api_data = []

        with open("Auth/configs.json") as f:
            self.configs = json.load(f)

        scope = 'https://www.googleapis.com/auth/admob.report'
        name = 'admob'
        version = 'v1'

        flow = OAuth2WebServerFlow(client_id=self.configs['client_id'], client_secret=self.configs['client_secret'],
                                   scope=scope)
        storage = Storage(name + '.dat')
        credentials = storage.get()
        if credentials is None or credentials.invalid:
            credentials = tools.run_flow(flow, storage)
        http = credentials.authorize(http=build_http())
        self.admob = discovery.build(name, version, http=http)
Beispiel #30
0
    def get_keys_from_gls(self, url):
        request = {
            "startDate":
            "2018-01-01",
            "endDate":
            datetime.today().strftime('%Y-%m-%d'),
            "dimensions": ["query"],
            "dimensionFilterGroups": [{
                "filters": [{
                    "dimension": "page",
                    "operator": "contains",
                    "expression": url
                }]
            }],
            'rowLimit':
            25000,
        }
        flow = client.flow_from_clientsecrets(
            CLIENT_SECRETS,
            scope=SCOPE_SEARCH,
            message=tools.message_if_missing(CLIENT_SECRETS))

        storage = file.Storage("other_files/webmasters.dat")
        credentials = storage.get()
        if credentials is None or credentials.invalid:
            credentials = tools.run_flow(flow, storage)
        http = credentials.authorize(http=build_http())
        try:
            service = build("webmasters", "v3", http=http)
            keys_list = []

            result = service.searchanalytics().query(
                siteUrl="https://redsale.by", body=request).execute()
            try:
                for row in result["rows"]:
                    keys_list.append(row["keys"][0])
            except KeyError:
                print("ошибкa!")
                print(result)

            return keys_list
        except:
            print("Ошибка подключения, переподключаюсь")
            self.get_keys_from_gls(url)
def document_api_from_discovery_document(discovery_url, doc_destination_dir):
    """Document the given API.

  Args:
    discovery_url (str): URI of discovery document.
    doc_destination_dir (str): relative path where the reference
        documentation should be saved.
  """
    http = build_http()
    response, content = http.request(discovery_url)
    discovery = json.loads(content)

    service = build_from_document(discovery)

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

    document_collection_recursive(
        service, "{}_{}.".format(name, version), discovery, discovery, doc_destination_dir
    )
Beispiel #32
0
    def __init__(self, config, log=print):
        self.config = config
        self.credential_dir = os.path.join(os.path.expanduser('.'),
                                           '.credential')
        self.credential_path = os.path.join(self.credential_dir,
                                            'credential.pkl')

        self.credentials = self.get_credentials()
        http = google_auth_httplib2.AuthorizedHttp(self.credentials,
                                                   http=build_http())
        self.service = discovery.build(API_SERVICE_NAME,
                                       API_VERSION,
                                       http=http)
        self.cache_play_list = {}
        self.cache_song_list = {}
        random.seed()
        self.thread = None
        self.queue = Queue()
        self.log = log
        self._enqueue_output()
Beispiel #33
0
def hello_get(key):
    """Responds with a form for editing user data."""

    # Look up any existing user data.
    calendar_storage = GoogleCalendarStorage(key)
    calendar_credentials = calendar_storage.get()

    # Force a Google Calendar credentials refresh to get the latest status.
    if calendar_credentials:
        try:
            calendar_credentials.refresh(build_http())
        except HttpAccessTokenRefreshError as e:
            error('Calendar token refresh error: %s' % e)
            calendar_storage.delete()
            calendar_credentials = None

    calendar_connected = calendar_credentials is not None
    return render_template(HELLO_TEMPLATE, key=key, user=Firestore().user(key),
                           calendar_connected=calendar_connected,
                           calendar_connect_url=google_calendar_step1(key),
                           calendar_disconnect_url=ACCOUNT_ACCESS_URL)
Beispiel #34
0
def blogger_service():
    CLIENT_SECRET_FILENAME = 'client_secret.json'
    CLIENT_SECRET_FILE = os.path.join(
        os.path.dirname(__file__), CLIENT_SECRET_FILENAME)
    SCOPE = 'https://www.googleapis.com/auth/blogger'  # Scope for Blogger R/W.
    flow = client.flow_from_clientsecrets(
        filename=CLIENT_SECRET_FILE,
        scope=SCOPE,
        message=tools.message_if_missing(CLIENT_SECRET_FILE)
    )
    credentials_data_path = os.path.join(
        os.path.dirname(__file__),
        'credentials',
        'blogger.dat')
    storage = file.Storage(credentials_data_path)
    credentials = storage.get()
    if credentials is None or credentials.invalid:
        credentials = tools.run_flow(flow, storage)
    http = credentials.authorize(http=build_http())
    blogger = build('blogger', 'v3', http=http)
    return blogger
def initialize_api(config_path, service_account_file):
  """Initialization of API service and configuration for the Content API.

  Args:
    config_path: string, relative path to a directory where configuration files
      exist.
    service_account_file: string, file name of the service account configuration
      file.

  Returns:
    A service object to interface with the Content API.
  """
  credentials = _authorize(config_path, service_account_file)
  auth_http = google_auth_httplib2.AuthorizedHttp(
      credentials,
      http=http.set_user_agent(http.build_http(), constants.APPLICATION_NAME))
  service = discovery.build(
      constants.SERVICE_NAME,
      constants.CONTENT_API_VERSION,
      http=auth_http,
      cache_discovery=False)
  return service
Beispiel #36
0
def build_from_document(
    service,
    base=None,
    future=None,
    http=None,
    developerKey=None,
    model=None,
    requestBuilder=HttpRequest,
    credentials=None
):
    """Create a Resource for interacting with an API.

  Same as `build()`, but constructs the Resource object from a discovery
  document that is it given, as opposed to retrieving one over HTTP.

  Args:
    service: string or object, the JSON discovery document describing the API.
      The value passed in may either be the JSON string or the deserialized
      JSON.
    base: string, base URI for all HTTP requests, usually the discovery URI.
      This parameter is no longer used as rootUrl and servicePath are included
      within the discovery document. (deprecated)
    future: string, discovery document with future capabilities (deprecated).
    http: httplib2.Http, An instance of httplib2.Http or something that acts
      like it that HTTP requests will be made through.
    developerKey: string, Key for controlling API usage, generated
      from the API Console.
    model: Model class instance that serializes and de-serializes requests and
      responses.
    requestBuilder: Takes an http request and packages it up to be executed.
    credentials: oauth2client.Credentials or
      google.auth.credentials.Credentials, credentials to be used for
      authentication.

  Returns:
    A Resource object with methods for interacting with the service.
  """

    if http is not None and credentials is not None:
        raise ValueError('Arguments http and credentials are mutually exclusive.')

    if isinstance(service, six.string_types):
        service = json.loads(service)

    if 'rootUrl' not in service and (isinstance(http, (HttpMock, HttpMockSequence))):
        logger.error(
            "You are using HttpMock or HttpMockSequence without" +
            "having the service discovery doc in cache. Try calling " +
            "build() without mocking once first to populate the " + "cache."
        )
        raise InvalidJsonError()

    base = urljoin(service['rootUrl'], service['servicePath'])
    schema = Schemas(service)

    # If the http client is not specified, then we must construct an http client
    # to make requests. If the service has scopes, then we also need to setup
    # authentication.
    if http is None:
        # Does the service require scopes?
        scopes = list(service.get('auth', {}).get('oauth2', {}).get('scopes', {}).keys())

        # If so, then the we need to setup authentication if no developerKey is
        # specified.
        if scopes and not developerKey:
            # If the user didn't pass in credentials, attempt to acquire application
            # default credentials.
            if credentials is None:
                credentials = _auth.default_credentials()

            # The credentials need to be scoped.
            credentials = _auth.with_scopes(credentials, scopes)

            # Create an authorized http instance
            http = _auth.authorized_http(credentials)

        # If the service doesn't require scopes then there is no need for
        # authentication.
        else:
            http = build_http()

    if model is None:
        features = service.get('features', [])
        model = JsonModel('dataWrapper' in features)

    return Resource(
        http=http,
        baseUrl=base,
        model=model,
        developerKey=developerKey,
        requestBuilder=requestBuilder,
        resourceDesc=service,
        rootDesc=service,
        schema=schema
    )
def init(argv, name, version, doc, filename, scope=None, parents=[], discovery_filename=None):
  """A common initialization routine for samples.

  Many of the sample applications do the same initialization, which has now
  been consolidated into this function. This function uses common idioms found
  in almost all the samples, i.e. for an API with name 'apiname', the
  credentials are stored in a file named apiname.dat, and the
  client_secrets.json file is stored in the same directory as the application
  main file.

  Args:
    argv: list of string, the command-line parameters of the application.
    name: string, name of the API.
    version: string, version of the API.
    doc: string, description of the application. Usually set to __doc__.
    file: string, filename of the application. Usually set to __file__.
    parents: list of argparse.ArgumentParser, additional command-line flags.
    scope: string, The OAuth scope used.
    discovery_filename: string, name of local discovery file (JSON). Use when discovery doc not available via URL.

  Returns:
    A tuple of (service, flags), where service is the service object and flags
    is the parsed command-line flags.
  """
  try:
      from oauth2client import client
      from oauth2client import file
      from oauth2client import tools
  except ImportError:
      raise ImportError('googleapiclient.sample_tools requires oauth2client. Please install oauth2client and try again.')

  if scope is None:
    scope = 'https://www.googleapis.com/auth/' + name

  # Parser command-line arguments.
  parent_parsers = [tools.argparser]
  parent_parsers.extend(parents)
  parser = argparse.ArgumentParser(
      description=doc,
      formatter_class=argparse.RawDescriptionHelpFormatter,
      parents=parent_parsers)
  flags = parser.parse_args(argv[1:])

  # Name of a file containing the OAuth 2.0 information for this
  # application, including client_id and client_secret, which are found
  # on the API Access tab on the Google APIs
  # Console <http://code.google.com/apis/console>.
  client_secrets = os.path.join(os.path.dirname(filename),
                                'client_secrets.json')

  # Set up a Flow object to be used if we need to authenticate.
  flow = client.flow_from_clientsecrets(client_secrets,
      scope=scope,
      message=tools.message_if_missing(client_secrets))

  # Prepare credentials, and authorize HTTP object with them.
  # If the credentials don't exist or are invalid run through the native client
  # flow. The Storage object will ensure that if successful the good
  # credentials will get written back to a file.
  storage = file.Storage(name + '.dat')
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    credentials = tools.run_flow(flow, storage, flags)
  http = credentials.authorize(http=build_http())

  if discovery_filename is None:
    # Construct a service object via the discovery service.
    service = discovery.build(name, version, http=http)
  else:
    # Construct a service object using a local discovery document file.
    with open(discovery_filename) as discovery_file:
      service = discovery.build_from_document(
          discovery_file.read(),
          base='https://www.googleapis.com/',
          http=http)
  return (service, flags)
    uri: string, URI of discovery document.
  """
  http = build_http()
  response, content = http.request(FLAGS.discovery_uri)
  discovery = json.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)


if __name__ == '__main__':
  FLAGS = parser.parse_args(sys.argv[1:])
  if FLAGS.discovery_uri:
    document_api_from_discovery_document(FLAGS.discovery_uri)
  else:
    http = build_http()
    resp, content = http.request(
        FLAGS.directory_uri,
        headers={'X-User-IP': '0.0.0.0'})
    if resp.status == 200:
      directory = json.loads(content)['items']
      for api in directory:
        document_api(api['name'], api['version'])
    else:
      sys.exit("Failed to load the discovery document.")
def init(argv, doc, parents=None, sandbox=False):
  """A common initialization routine for the Content API samples.

  Args:
    argv: list of string, the command-line parameters of the application.
    doc: string, description of the application. Usually set to __doc__.
    parents: list of argparse.ArgumentParser, additional command-line flags.
    sandbox: boolean, whether to use the sandbox API endpoint or not.

  Returns:
    A tuple of (service, config, flags), where service is the service object,
    config is the configuration JSON in Python form, and flags
    are the parsed command-line flags.
  """
  service = None
  sandbox_service = None
  flags = None
  parent_parsers = []
  if parents is not None:
    parent_parsers.extend(parents)

  parser = argparse.ArgumentParser(
      description=doc,
      formatter_class=argparse.RawDescriptionHelpFormatter,
      parents=parent_parsers)
  parser.add_argument(
      '--config_path',
      metavar='PATH',
      default=os.path.expanduser('~/shopping-samples'),
      help='configuration directory for the Shopping samples')
  parser.add_argument(
      '--noconfig',
      action='store_true',
      help='run samples with no configuration directory')
  parser.add_argument(
      '--log_file',
      metavar='FILE',
      help='filename for logging API requests and responses'
  )
  flags = parser.parse_args(argv[1:])

  if flags.log_file:
    logging.basicConfig(filename=flags.log_file, level=logging.INFO)
    model.dump_request_response = True

  config = {}
  if not flags.noconfig:
    if not os.path.isdir(flags.config_path):
      print(
          'Configuration directory "%s" does not exist.' % flags.config_path,
          file=sys.stderr)
      sys.exit(1)

    content_path = os.path.join(flags.config_path, 'content')
    if not os.path.isdir(content_path):
      print(
          'Content API configuration directory "%s" does not exist.' %
          content_path,
          file=sys.stderr)
      sys.exit(1)

    config_file = os.path.join(content_path, 'merchant-info.json')
    if not os.path.isfile(config_file):
      print('Configuration file %s does not exist.' % config_file)
      print('Falling back to configuration based on authenticated user.')
    else:
      config = json.load(open(config_file, 'r'))
    config['path'] = content_path

  credentials = auth.authorize(config)
  auth_http = google_auth_httplib2.AuthorizedHttp(
      credentials, http=http.set_user_agent(
          http.build_http(), _constants.APPLICATION_NAME))
  if _constants.ENDPOINT_ENV_VAR in os.environ:
    # Strip off everything after the host/port in the URL.
    root_url = six.moves.urllib.parse.urljoin(
        os.environ[_constants.ENDPOINT_ENV_VAR],
        '/')
    print('Using non-standard root for API discovery: %s' % root_url)
    discovery_url = root_url + '/discovery/v1/apis/{api}/{apiVersion}/rest'
    service = discovery.build(
        _constants.SERVICE_NAME,
        _constants.SERVICE_VERSION,
        discoveryServiceUrl=discovery_url,
        http=auth_http)
    if sandbox:
      sandbox_service = discovery.build(
          _constants.SERVICE_NAME,
          _constants.SANDBOX_SERVICE_VERSION,
          discoveryServiceUrl=discovery_url,
          http=auth_http)
  else:
    service = discovery.build(
        _constants.SERVICE_NAME, _constants.SERVICE_VERSION, http=auth_http)
    if sandbox:
      sandbox_service = discovery.build(
          _constants.SERVICE_NAME,
          _constants.SANDBOX_SERVICE_VERSION,
          http=auth_http)

  # Now that we have a service object, fill in anything missing from the
  # configuration using API calls.
  retrieve_remaining_config_from_api(service, config)

  return (sandbox_service if sandbox else service, config, flags)
Beispiel #40
0
def build(
    serviceName,
    version,
    http=None,
    discoveryServiceUrl=DISCOVERY_URI,
    developerKey=None,
    model=None,
    requestBuilder=HttpRequest,
    credentials=None,
    cache_discovery=True,
    cache=None
):
    """Construct a Resource for interacting with an API.

  Construct a Resource object for interacting with an API. The serviceName and
  version are the names from the Discovery service.

  Args:
    serviceName: string, name of the service.
    version: string, the version of the service.
    http: httplib2.Http, An instance of httplib2.Http or something that acts
      like it that HTTP requests will be made through.
    discoveryServiceUrl: string, a URI Template that points to the location of
      the discovery service. It should have two parameters {api} and
      {apiVersion} that when filled in produce an absolute URI to the discovery
      document for that service.
    developerKey: string, key obtained from
      https://code.google.com/apis/console.
    model: googleapiclient.Model, converts to and from the wire format.
    requestBuilder: googleapiclient.http.HttpRequest, encapsulator for an HTTP
      request.
    credentials: oauth2client.Credentials or
      google.auth.credentials.Credentials, credentials to be used for
      authentication.
    cache_discovery: Boolean, whether or not to cache the discovery doc.
    cache: googleapiclient.discovery_cache.base.CacheBase, an optional
      cache object for the discovery documents.

  Returns:
    A Resource object with methods for interacting with the service.
  """
    params = {'api': serviceName, 'apiVersion': version}

    if http is None:
        discovery_http = build_http()
    else:
        discovery_http = http

    for discovery_url in (
        discoveryServiceUrl,
        V2_DISCOVERY_URI,
    ):
        requested_url = uritemplate.expand(discovery_url, params)

        try:
            content = _retrieve_discovery_doc(requested_url, discovery_http, cache_discovery, cache)
            return build_from_document(
                content,
                base=discovery_url,
                http=http,
                developerKey=developerKey,
                model=model,
                requestBuilder=requestBuilder,
                credentials=credentials
            )
        except HttpError as e:
            if e.resp.status == http_client.NOT_FOUND:
                continue
            else:
                raise e

    raise UnknownApiNameOrVersion("name: %s  version: %s" % (serviceName, version))
 def test_build_http_sets_default_timeout_if_none_specified(self):
   socket.setdefaulttimeout(None)
   http = build_http()
   self.assertIsInstance(http.timeout, int)
   self.assertGreater(http.timeout, 0)
 def test_build_http_default_timeout_can_be_overridden(self):
   socket.setdefaulttimeout(1.5)
   http = build_http()
   self.assertAlmostEqual(http.timeout, 1.5, delta=0.001)
 def test_build_http_default_timeout_can_be_set_to_zero(self):
   socket.setdefaulttimeout(0)
   http = build_http()
   self.assertEquals(http.timeout, 0)