c# - Google 日历活动未显示在日历中
问题描述
我们在 Google 中创建了一个服务帐户并使用 Calendar API 添加事件,之前它工作正常,在 google 停止该帐户并重新激活它之后,它没有工作
我们已经尝试了新的服务帐户并逐行调试我们的代码并且没有错误并且还返回创建的事件但没有显示在日历中
CalendarService service = null;
var serviceAccountCredentialFilePath =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
"ServiceAccount_Key.json");
if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower()
== ".json")
{
GoogleCredential credential;
string[] scopes = new string[] {
CalendarService.Scope.Calendar, // Manage your calendars
CalendarService.Scope.CalendarReadonly // View your Calendars
};
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes);
}
// Create the service.
service = new CalendarService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Esoco"
});
}
// End //
// Insert Event //
var myEvent = new Event
{
Id = guid,
//Transparency = "OPAQUE",
Summary = subject,
Location = location,
Start = new EventDateTime { TimeZone = timezone, DateTime = start },
End = new EventDateTime { TimeZone = timezone, DateTime = end },
Attendees = objattendees,
Organizer = organizerdata,
Description = summary,
};
myEvent.Start.DateTimeRaw = myEvent.Start.DateTimeRaw.Replace("Z", "");
myEvent.End.DateTimeRaw = myEvent.End.DateTimeRaw.Replace("Z", "");
//myEvent.ICalUID
var recurringEvent = service.Events.Insert(myEvent, "primary");
recurringEvent.SendNotifications = true;
try
{
var outputofevent = recurringEvent.Execute();
ScriptManager.RegisterClientScriptBlock(this, typeof(string), "Alertscript", "alert('"+outputofevent.Status+"')", true);
}
预期结果在日历中插入事件显示,但实际结果未在日历中显示
解决方案
在过去的几周里,我的团队遇到了同样的问题,这就是我团队的解决方法。
我们的场景
- 我们创建一个名为
google-calendar@mycompany.iam.gserviceaccount.com
- 我们使用 1) 中的服务帐户凭据用于 Google Calendar API
- 在服务帐户的日历中创建一个事件,如下面的代码
calendar.events.insert({
calendarId: 'primary', // it will create an event into service account's calendar
resource: data,
sendNotifications: true
})
我们为解决方法所做的工作。
- 创建一个新公司的用户(比方说
system@mycompany.com
) - 与服务帐户共享
system@mycompany.com
日历- 转到“日历>设置>与特定人共享”然后
- 添加
google-calendar@mycompany.iam.gserviceaccount.com
“对事件进行更改”
- 将代码从创建事件更新到服务帐户的日历到刚刚创建的用户的日历,如下所示
calendar.events.insert({
calendarId: 'system@mycompany.com', // << we changed from 'primary' to just-created-user
resource: data,
sendNotifications: true
})
更改后,创建的事件将按原样显示在 Google 日历中。
希望这可以帮助
推荐阅读
- identityserver4 - 是否有描述 IdentityServer4 数据表和列如何协同工作的好文章/文档?
- sql - 计算分工工时?
- java - 使用用户输入的 Java 链表
- hedera-hashgraph - Hedera 共识服务中单个主题的多个提交密钥
- kubernetes - Kubernetes:验证错误(Deployment.spec.template.spec.container[0]):io.k8s.api.core.v1.Container 中的未知字段“ConfigMapref”
- python - 从光栅图像中提取街道网络
- python - 在滥用设置中使用 tf.function 时出现罕见错误
- python - Scipy Lagrange 除以零
- javascript - 使用 javascript 中的正则表达式在文本框中输入文本时格式化许可证号
- c# - 使用绑定参数更新模型会使那些不在形式中的模型无效