【问题标题】:Google Calendars API - Insert Event - Notify organizer by emailGoogle Calendars API - 插入事件 - 通过电子邮件通知组织者
【发布时间】:2020-10-28 10:44:09
【问题描述】:

使用 Google Calendars API /events/insert,我代表用户在用户的日历中创建了一个活动,并将他们设置为组织者。我还邀请了一位客人。

我希望组织者收到类似于客人可能收到的电子邮件通知。我尝试使用 sendUpdates 参数,但它只通知客人。

有没有办法通知组织者在他们的日历中代表他们创建了一个活动?

【问题讨论】:

    标签: google-calendar-api


    【解决方案1】:

    您可以做的一个解决方法是向与会者添加另一个版本的所有者电子邮件。例如,如果他们的普通电子邮件是test@gmail.com,您可以将其转换为test+1@gmail.com。他们将通过普通电子邮件收到邀请。请记住,这将为活动添加额外的参与者。当他们在电子邮件中单击接受时,它会将他们的原始电子邮件标记为已接受,而不是附加了+1 的电子邮件。

    在 JS 中添加+1 的电子邮件请参见此基本功能:

    function changeEmail(email) {
        let splitEmail = email.split('@');
        return splitEmail[0] + '+1@' + splitEmail[1];
      }
    

    请注意,您可以在电子邮件后添加任何文本。不一定非要 +1。

    【讨论】:

      【解决方案2】:

      您需要考虑一下 Oauth2 在做什么。它授予您的应用程序代表用户执行操作的权限。 API 无法知道这不是由 User1 执行的操作。如果 User1 正在执行一项操作,则不需要 User1 通知他们正在执行某项操作。

      API 不会告诉别人他们刚刚插入了一些他们已经知道他们做了的东西。如果您想通知他们,我认为您的应用程序需要自己发送消息。

      您可以尝试让日历的所有者作为活动的参与者,这可能会向他们发送一条消息,但我真的不记得这是否不是自动添加的。 Google 最近对因垃圾邮件问题而被添加到活动中时通知电子邮件的发送方式进行了一些更改。

      【讨论】:

        【解决方案3】:

        答案:

        正如DaImTo 所说,API 不会向创建事件的用户提供电子邮件,这正是您的应用程序通过 OAuth2 执行的操作。

        但是,您可以从响应中获取事件信息,并使用 Gmail API 手动向用户发送电子邮件。

        示例代码:

        我不知道您使用的是什么语言,但作为 python 示例,在使用您的服务帐户凭据获取 Gmail 服务后:

        subject = "email@example.com"
        delegated_credentials = service_account.Credentials.from_service_account_file(key_file_location, scopes=scopes, subject=subject)
        
        gmail_service = discovery.build(api_name, api_version, credentials=delegated_credentials)
        

        您可以使用Sending Email 教程代表创建事件的用户创建和发送电子邮件:

        # Copyright 2020 Google LLC.
        # SPDX-License-Identifier: Apache-2.0
        
        def create_message(sender, to, subject, message_text):
          message = MIMEText(message_text)
          message['to'] = to
          message['from'] = sender
          message['subject'] = subject
          return {'raw': base64.urlsafe_b64encode(message.as_string())}
        
        def send_message(service, user_id, message):
          try:
            message = (service.users().messages().send(userId=user_id, body=message)
                       .execute())
            print 'Message Id: %s' % message['id']
            return message
          except errors.HttpError, error:
            print 'An error occurred: %s' % error
        

        然后使用已为事件插入调用定义的数据通过电子邮件将信息发送给用户:

        emailText = "Title: " + summary + "\nStart: " + startTime + "\nEnd: " + endTime
        
        msg = create_message(subject, # the sub for the service account
                             subject, #to and from are the same
                             "A Calendar Event has been created on your behalf",
                             emailText)
        
        send_message(gmail_service, calendar_response['creator']['email']
                             
        

        参考资料:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-11-15
          • 2021-04-18
          • 2022-01-10
          • 1970-01-01
          • 1970-01-01
          • 2018-01-09
          • 2023-04-03
          • 2015-08-29
          相关资源
          最近更新 更多