示例#1
0
  def Send(self, message):
    """Send an e-mail via Gmail API.

    Args:
      message: A MIMEMultipart() object containing the body of the message.

    Returns:
      True if the email was sent, else False.
    """
    if not apiclient_build:
      logging.warning('Could not send email: Google API client not installed.')
      return False

    try:
      credentials = self._GetCachedCredentials()
    except AuthenticationError as e:
      logging.warning('Could not get gmail credentials: %s', e)
      return False

    http = credentials.authorize(httplib2.Http())
    service = apiclient_build('gmail', 'v1', http=http)
    try:
      # 'me' represents the default authorized user.
      payload = {'raw': base64.urlsafe_b64encode(message.as_string())}
      service.users().messages().send(userId='me', body=payload).execute()
      return True
    except apiclient_errors.HttpError as error:
      logging.warning('Could not send email: %s', error)
      return False
    def __init__(self, oauth_credentials, project_name,
                 monorail_server='staging'):
        if apiclient_build is None:
            raise ProjectHostingApiException('Cannot get apiclient library.')

        if not oauth_credentials:
            raise ProjectHostingApiException('No oauth_credentials is provided.')

        # TODO(akeshet): This try-except is due to incompatibility of phapi_lib
        # with oauth2client > 2. Until this is fixed, this is expected to fail
        # and bug filing will be effectively disabled. crbug.com/648489
        try:
          storage = oauth_client_fileio.Storage(oauth_credentials)
          credentials = storage.get()
        except Exception as e:
          raise ProjectHostingApiException('Incompaible credentials format, '
                                           'or other exception. Will not file '
                                           'bugs.')
        if credentials is None or credentials.invalid:
            raise ProjectHostingApiException('Invalid credentials for Project '
                                             'Hosting api. Cannot file bugs.')

        http = credentials.authorize(httplib2.Http())
        try:
            url = ('https://monorail-%s.appspot.com/_ah/api/discovery/v1/'
                   'apis/{api}/{apiVersion}/rest' % monorail_server)
            self._codesite_service = apiclient_build(
                "monorail", "v1", http=http,
                discoveryServiceUrl=url)
        except (apiclient_errors.Error, httplib2.HttpLib2Error,
                httplib.BadStatusLine) as e:
            raise ProjectHostingApiException(str(e))
        self._project_name = project_name
示例#3
0
文件: alerts.py 项目: sjg20/chromite
  def Send(self, message):
    """Send an e-mail via Gmail API.

    Args:
      message: A MIMEMultipart() object containing the body of the message.

    Returns:
      True if the email was sent, else False.
    """
    if not apiclient_build:
      logging.warning('Could not send email: Google API client not installed.')
      return False

    try:
      credentials = self._GetCachedCredentials()
    except AuthenticationError as e:
      logging.warning('Could not get gmail credentials: %s', e)
      return False

    http = credentials.authorize(httplib2.Http())
    service = apiclient_build('gmail', 'v1', http=http)
    try:
      # 'me' represents the default authorized user.
      payload = {'raw': base64.urlsafe_b64encode(message.as_string())}
      service.users().messages().send(userId='me', body=payload).execute()
      return True
    except apiclient_errors.HttpError as error:
      logging.warning('Could not send email: %s', error)
      return False
示例#4
0
 def __login(self):
     # Authenticate to GMail using OAuth; store the token for reuse
     store = oauth_file.Storage('token.json')
     creds = store.get()
     if not creds or creds.invalid:
         flow = client.flow_from_clientsecrets('credentials.json',
                                               self.scope)
         creds = tools.run_flow(flow, store)
     self.api_service = apiclient_build('gmail',
                                        'v1',
                                        http=creds.authorize(Http()))
     print(self.api_service)
     return self
    def __init__(self, oauth_credentials):
        """Init Gmail API client

        @param oauth_credentials: Path to the oauth credential token.
        """
        if not apiclient_build:
            raise GmailApiException('Cannot get apiclient library.')

        storage = oauth_client_fileio.Storage(oauth_credentials)
        credentials = storage.get()
        if not credentials or credentials.invalid:
            raise GmailApiException('Invalid credentials for Gmail API, '
                                    'could not send email.')
        http = credentials.authorize(httplib2.Http())
        self._service = apiclient_build('gmail', 'v1', http=http)
    def __init__(self, oauth_credentials, project_name):
        if apiclient_build is None:
            raise ProjectHostingApiException('Cannot get apiclient library.')

        if not oauth_credentials:
            raise ProjectHostingApiException(
                'No oauth_credentials is provided.')

        storage = oauth_client_fileio.Storage(oauth_credentials)
        credentials = storage.get()
        if credentials is None or credentials.invalid:
            raise ProjectHostingApiException('Invalid credentials for Project '
                                             'Hosting api. Cannot file bugs.')

        http = credentials.authorize(httplib2.Http())
        try:
            self._codesite_service = apiclient_build('projecthosting',
                                                     'v2',
                                                     http=http)
        except (apiclient_errors.Error, httplib2.HttpLib2Error,
                httplib.BadStatusLine) as e:
            raise ProjectHostingApiException(str(e))
        self._project_name = project_name