c# - Google Calendar API 为不同的请求返回 404(未找到)
问题描述
我在 .NET 中实现了一个应用程序,它将 Google 日历事件与我的数据库事件同步。它工作正常,直到我需要使用需要eventId作为参数的相同 Google API 方法,如delete、move、update。我的问题是这种方法有时有效,有时无效。我无法弄清楚真正的问题是什么以及在什么情况下会产生错误。我还在Google Calendar API 文档中使用了OAuth 2.0 Playground和“试用此 API”选项进行测试,但没有成功(仍然是 404 错误)。
我在 Google 中有多个日历,由我创建并以完全权限为我共享。无论我是否是日历的所有者,这些问题都会毫无逻辑地出现。我使用的范围是:CalendarService.Scope.Calendar、CalendarService.Scope.CalendarEvents。
使用我的应用从 Google 日历中删除活动。
它给了我 404。接下来,我尝试删除Google Calendar API 文档中的同一事件 - Events: delete。仍然是 404。我使用Google OAuth requests验证了该事件是否存在。令人惊讶的是它存在。好的,让我们再次尝试删除它。同样的错误。如果我没有失去耐心并尝试了足够多的方法,那么总有一天一切都会好起来的。
更新事件。
同样的问题:没有找到。
将事件从一个日历移动到另一个日历。
同样的问题:没有找到。但在这种情况下,我注意到使用Google Calendar API 文档的不同之处 - 事件:移动“尝试此 API”选项:在我尝试将事件从我的一个日历(所有者)移动到另一个我的日历之前,它不起作用(所有者)。从这一刻起,一切正常,没有其他错误(即使是共享日历)。
日历服务.cs
public CalendarService GetService()
{
try
{
// Authenticated user credential
var credential = GetUserCredential();
if (credential == null)
throw new ArgumentNullException("credential");
// Create Calendar API service.
return new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName
});
}
catch (Exception ex)
{
throw new Exception("Get Calendar service failed.", ex);
}
}
private UserCredential GetUserCredential()
{
try
{
UserCredential credential;
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
string credPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
userId,
CancellationToken.None,
new FileDataStore(credPath, true)
).Result;
}
credential.GetAccessTokenForRequestAsync();
return credential;
}
catch (Exception ex)
{
throw new Exception("Get user credentials failed.", ex);
}
}
public async Task MoveEvent(CalendarService service, string calendarId, string eventId, string destination)
{
// Building the initial request & requesting data async
await service.Events.Move(calendarId, eventId, destination).ExecuteAsync();
}
日历逻辑.cs
private GoogleCalendarData calendarData = new GoogleCalendarData();
private GoogleCalendarService calendarService = new GoogleCalendarService();
public async Task MoveAppEvents(string sourceCalendarId, string destinationCalendarId)
{
//Here events from database inserted in Google are retrieved
var events = await calendarData.GetEvents(null, false);
events.Where(item => item.GoogleEventId != "")
.ToList()
.ForEach((async item => await calendarService.MoveEvent(calendarService.GetService(), sourceCalendarId, item.GoogleEventId, destinationCalendarId)));
}
日历控制器.cs
private GoogleCalendarLogic googleCalendar = new GoogleCalendarLogic();
[Route("{calendarId}/appevents/move/{destination}")]
[HttpPost]
public async Task MoveAppEvents(string calendarId, string destination)
{
await googleCalendar.MoveAppEvents(calendarId, destination);
}
解决方案
推荐阅读
- string - Scala如何从CSV中的特定列中查找所有唯一值?
- java - Weka Experimenter mod:创建要运行的实验副本时出现问题
- javascript - 调用函数时如何循环此 if 语句
- python - 如何在具有分类变量和连续变量的数据集中查找和计算相关性?
- android - Flutter App 在 Android 上因使用 Firebase Firestore 匿名登录而崩溃
- ios - 可以自定义 ASAuthorizationAppleIDButton 的边框吗?
- java - 如何获得差异最小的值的索引?
- javascript - 在我的 Javascript 中未检测到 EventListener
- android - Android - 如果使用 androidx,则不应用可访问性字体大小的系统设置
- python - 如何在 VSCode 中关闭此功能提示?