如何解决Google Calendars API-插入事件-通过电子邮件通知组织者 答案:示例代码:参考文献:
我使用Google Calendars API /events/insert,代表用户在用户日历中创建一个活动,并将其设置为组织者。我还邀请了一位客人。
我希望组织者收到类似于客人可能收到的电子邮件通知。我尝试使用sendUpdates参数,但它仅通知来宾。
是否可以通知组织者代表他们在日历中创建了活动?
解决方法
您需要考虑一下Oauth2在做什么。它授予您的应用程序许可以代表用户执行操作。 API无法知道这不是执行操作的User1。如果User1正在执行操作,则不需要User1通知他们正在做某事。
API不会告诉别人他们刚刚插入了已经知道自己做了的东西。如果您想通知他们,我认为您的应用程序将需要自行发送消息。
您可以尝试将日历的所有者作为活动的参加者,这可能会向他们发送消息,我真的不记得这是否是自动添加的内容。由于垃圾邮件的某些问题,Google最近对将某人添加到活动中时通知电子邮件的发送方式进行了一些更改。
,答案:
正如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']
我希望这对您有帮助!
参考文献:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。