Bha*_*man 3 c# google-calendar-api google-api google-api-dotnet-client service-accounts
我们在 Google 中创建了一个服务帐户并使用日历 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);
}
Run Code Online (Sandbox Code Playgroud)
预期结果已插入日历中的事件显示,但实际结果未显示在日历中
在过去的几周里,我的团队遇到了同样的问题,这就是我的团队的解决方法。
我们的场景
google-calendar@mycompany.iam.gserviceaccount.com
calendar.events.insert({
calendarId: 'primary', // it will create an event into service account's calendar
resource: data,
sendNotifications: true
})
Run Code Online (Sandbox Code Playgroud)
我们为解决方法所做的工作。
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
})
Run Code Online (Sandbox Code Playgroud)
更改后,创建的事件将按其应有的方式显示在 Google 日历中。
希望这可以帮助
归档时间: |
|
查看次数: |
1846 次 |
最近记录: |