def __init__(self, keyfile=None, email=None, profile_id=None):
        """
        :param keyfile:
            Path to the PKCS 12 file for authenticating with the API as
            a service account. Defaults to settings.GA_API_KEYFILE.

        :param email:
            Email address for the service account to authenticate as.
            Defaults to settings.GA_API_ACCOUNT_EMAIL.

        :param profile_id:
            ID of analytics profile to query. Defaults to
            settings.GA_API_PROFILE_ID.
        """
        self.profile_id = profile_id or settings.GA_API_PROFILE_ID
        keyfile = keyfile or settings.GA_API_KEYFILE

        try:
            with open(keyfile, 'rb') as f:
                key = f.read()
        except IOError as e:
            raise AnalyticsError('Could not read keyfile `{0}`: {1}'.format(keyfile, e), e)

        email = email or settings.GA_API_ACCOUNT_EMAIL
        credentials = SignedJwtAssertionCredentials(
            email, key, scope='https://www.googleapis.com/auth/analytics.readonly')
        http = httplib2.Http()
        http = credentials.authorize(http)

        try:
            self._service = build_service('analytics', 'v3', http=http)
        except OAuth2Error as e:
            raise AnalyticsError('Error authenticating with Analytics API: {0}'.format(e), e)
    def get(self):
        callback = self.request.host_url + '/oauth2callback'
        flow = OAuth2WebServerFlow(client_id=CLIENT_ID,
                                   client_secret=CLIENT_SECRET,
                                   redirect_uri=callback,
                                   scope=SCOPE,
                                   user_agent=USER_AGENT)

        # Exchange the code (in self.request.params) for an access token.
        credentials = flow.step2_exchange(self.request.params)
        http = credentials.authorize(httplib2.Http())

        bucket = self.request.get('state')
        storage = build_service('storage', 'v1beta1', http=http)
        list_response = storage.objects().list(bucket=bucket,
                                               fields=FIELDS).execute()
        template_values = {
            'items': list_response['items'],
            'bucket_name': bucket
        }

        # We use a templating engine to format our output. For more information:
        #   <http://jinja.pocoo.org/docs/>
        jinja_env = jinja2.Environment(
            loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
        template = jinja_env.get_template('listing.html')
        self.response.out.write(template.render(template_values))
Example #3
0
def list_bucket_contents(bucket, subdir=None):
  """ Returns files in the Google Cloud Storage bucket as a (dirs, files) tuple.

  Uses the API documented at
  https://developers.google.com/storage/docs/json_api/v1/objects/list

  Args:
    bucket: name of the Google Storage bucket
    subdir: directory within the bucket to list, or None for root directory
  """
  # The GCS command relies on the subdir name (if any) ending with a slash.
  if subdir and not subdir.endswith('/'):
    subdir += '/'
  subdir_length = len(subdir) if subdir else 0

  storage = build_service('storage', 'v1')
  command = storage.objects().list(
      bucket=bucket, delimiter='/', fields='items(name),prefixes',
      prefix=subdir)
  results = command.execute()

  # The GCS command returned two subdicts:
  # prefixes: the full path of every directory within subdir, with trailing '/'
  # items: property dict for each file object within subdir
  #        (including 'name', which is full path of the object)
  dirs = []
  for dir_fullpath in results.get('prefixes', []):
    dir_basename = dir_fullpath[subdir_length:]
    dirs.append(dir_basename[:-1])  # strip trailing slash
  files = []
  for file_properties in results.get('items', []):
    file_fullpath = file_properties['name']
    file_basename = file_fullpath[subdir_length:]
    files.append(file_basename)
  return (dirs, files)
Example #4
0
def list_bucket_contents(bucket, subdir=None):
  """ Returns files in the Google Cloud Storage bucket as a (dirs, files) tuple.

  Uses the API documented at
  https://developers.google.com/storage/docs/json_api/v1/objects/list

  Args:
    bucket: name of the Google Storage bucket
    subdir: directory within the bucket to list, or None for root directory
  """
  # The GCS command relies on the subdir name (if any) ending with a slash.
  if subdir and not subdir.endswith('/'):
    subdir += '/'
  subdir_length = len(subdir) if subdir else 0

  storage = build_service('storage', 'v1')
  command = storage.objects().list(
      bucket=bucket, delimiter='/', fields='items(name),prefixes',
      prefix=subdir)
  results = command.execute()

  # The GCS command returned two subdicts:
  # prefixes: the full path of every directory within subdir, with trailing '/'
  # items: property dict for each file object within subdir
  #        (including 'name', which is full path of the object)
  dirs = []
  for dir_fullpath in results.get('prefixes', []):
    dir_basename = dir_fullpath[subdir_length:]
    dirs.append(dir_basename[:-1])  # strip trailing slash
  files = []
  for file_properties in results.get('items', []):
    file_fullpath = file_properties['name']
    file_basename = file_fullpath[subdir_length:]
    files.append(file_basename)
  return (dirs, files)
Example #5
0
def get_all_projects_in_folder(folder_id=None):
    if folder_id is None:
        return

    if not folder_id.startswith("folders/"):
        folder_id = "folders/" + folder_id

    crm = build_service("cloudresourcemanager", "v2")
    allFolders = get_all_folders_in_folder(crm, folder_id)
    crm.close()
    allFolders.append(folder_id)

    project_crm = build_service("cloudresourcemanager", "v1")
    allProjects = []
    for folder in allFolders:
        projects = (project_crm.projects().list(filter="parent.id:" +
                                                folder[8:]).execute())
        if projects:
            allProjects.append(projects)
    project_crm.close()

    # flatten and return
    return sum([p["projects"] for p in allProjects], [])
Example #6
0
    def __init__(self, keyfile=None, email=None, profile_id=None):
        """
        :param keyfile:
            Path to the PKCS 12 file for authenticating with the API as
            a service account. Defaults to settings.GA_API_KEYFILE.

        :param email:
            Email address for the service account to authenticate as.
            Defaults to settings.GA_API_ACCOUNT_EMAIL.

        :param profile_id:
            ID of analytics profile to query. Defaults to
            settings.GA_API_PROFILE_ID.
        """
        self.profile_id = profile_id or settings.GA_API_PROFILE_ID
        keyfile = keyfile or settings.GA_API_KEYFILE

        try:
            with open(keyfile, 'rb') as f:
                key = f.read()
        except IOError as e:
            raise AnalyticsError(
                'Could not read keyfile `{0}`: {1}'.format(keyfile, e), e)

        email = email or settings.GA_API_ACCOUNT_EMAIL
        credentials = SignedJwtAssertionCredentials(
            email,
            key,
            scope='https://www.googleapis.com/auth/analytics.readonly')
        http = httplib2.Http()
        http = credentials.authorize(http)

        try:
            self._service = build_service('analytics', 'v3', http=http)
        except OAuth2Error as e:
            raise AnalyticsError(
                'Error authenticating with Analytics API: {0}'.format(e), e)
  def get(self):
    callback = self.request.host_url + '/oauth2callback'
    flow = OAuth2WebServerFlow(client_id=CLIENT_ID,
                               client_secret=CLIENT_SECRET,
                               redirect_uri=callback,
                               scope=SCOPE,
                               user_agent=USER_AGENT)

    # Exchange the code (in self.request.params) for an access token.
    credentials = flow.step2_exchange(self.request.params)
    http = credentials.authorize(httplib2.Http())

    bucket = self.request.get('state')
    storage = build_service('storage', 'v1beta1', http=http)
    list_response = storage.objects().list(bucket=bucket,
                                           fields=FIELDS).execute()
    template_values = {'items': list_response['items'], 'bucket_name': bucket}

    # We use a templating engine to format our output. For more information:
    #   <http://jinja.pocoo.org/docs/>
    jinja_env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
    template = jinja_env.get_template('listing.html')
    self.response.out.write(template.render(template_values))
 def _service(self, product, version="v1"):
     """Internal helper around google client lib's build service func"""
     return build_service(product, version)
Example #9
0
        except oauth2client.clientsecrets.InvalidClientSecretsError, e:
            print "error: couldn't read client secrets: ", e
            exit(1)

        # See https://developers.google.com/api-client-library/python/guide/aaa_oauth#oauth2client
        credentials = run(flow, storage)

    if credentials is None:
        print "error: couldn't get authorization from Google Drive"
        exit(1)

    # Create an httplib2.Http object and authorize it with our credentials
    http = httplib2.Http()
    http = credentials.authorize(http)

    drive_service = build_service('drive', 'v2', http=http)
    return drive_service

def find_subclasses_of(base_class):
    import types

    for name in globals():
        obj = globals()[name]
        # Find all subclasses of 
        if obj != Command and type(obj) == types.ClassType and issubclass(obj, Command):
            yield obj

def build_commands_list():
    """Search the global namespace for Command classes, and return them in a dictionary keyed on their names."""
    commands = [command_class() for command_class in find_subclasses_of(Command)]
    return {command.name: command for command in commands}
Example #10
0
 def build_service(self):
     return build_service('drive', 'v2', http=self.auth_mgr.get_auth_http())
Example #11
0
 def build_service(self):
     return build_service('drive', 'v2', http=self.auth_mgr.get_auth_http())