Ejemplo n.º 1
0
    def __init__(self,
                 host,
                 client_email,
                 client_private_key,
                 private_key_password='******',
                 user_agent=None,
                 timeout=None,
                 extra_headers=None):
        """Wrapper around httplib2.Http() that handles authentication.

    client_email: email associated with the service account
    client_private_key: encrypted private key, as a string
    private_key_password: password used to decrypt the private key
    """

        # Enforce https
        host_parts = urlparse.urlparse(host)

        if host_parts.scheme == 'https':  # fine
            self.host = host
        elif host_parts.scheme == 'http':
            upload.logging.warning('Changing protocol to https')
            self.host = 'https' + host[4:]
        else:
            msg = 'Invalid url provided: %s' % host
            upload.logging.error(msg)
            raise ValueError(msg)

        self.host = self.host.rstrip('/')

        self.extra_headers = extra_headers or {}

        if not oa2client.HAS_OPENSSL:
            logging.error("No support for OpenSSL has been found, "
                          "OAuth2 support requires it.")
            logging.error(
                "Installing pyopenssl will probably solve this issue.")
            raise RuntimeError('No OpenSSL support')
        self.creds = oa2client.SignedJwtAssertionCredentials(
            client_email,
            client_private_key,
            'https://www.googleapis.com/auth/userinfo.email',
            private_key_password=private_key_password,
            user_agent=user_agent)

        self._http = self.creds.authorize(httplib2.Http(timeout=timeout))
Ejemplo n.º 2
0
def _get_oauth2_client(service_account_json_file, scopes):
  with open(service_account_json_file) as f:
    service_account_json = json.load(f)
  return client.SignedJwtAssertionCredentials(
      service_account_json['client_email'], service_account_json['private_key'],
      scopes)