首页 > 解决方案 > Google Calendar API 为不同的请求返回 404(未找到)

问题描述

我在 .NET 中实现了一个应用程序,它将 Google 日历事件与我的数据库事件同步。它工作正常,直到我需要使用需要eventId作为参数的相同 Google API 方法,如deletemoveupdate。我的问题是这种方法有时有效,有时无效。我无法弄清楚真正的问题是什么以及在什么情况下会产生错误。我还在Google Calendar API 文档中使用了OAuth 2.0 Playground和“试用此 API”选项进行测试,但没有成功(仍然是 404 错误)。

我在 Google 中有多个日历,由我创建并以完全权限为我共享。无论我是否是日历的所有者,这些问题都会毫无逻辑地出现。我使用的范围是:CalendarService.Scope.Calendar、CalendarService.Scope.CalendarEvents。

  1. 使用我的应用从 Google 日历中删除活动。

    它给了我 404。接下来,我尝试删除Google Calendar API 文档中的同一事件 - Events: delete仍然是 404。我使用Google OAuth requests验证了该事件是否存在。令人惊讶的是它存在。好的,让我们再次尝试删除它。同样的错误。如果我没有失去耐心并尝试了足够多的方法,那么总有一天一切都会好起来的。

  2. 更新事件

    同样的问题:没有找到。

  3. 将事件从一个日历移动到另一个日历。

    同样的问题:没有找到。但在这种情况下,我注意到使用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);
}

标签: c#.netgoogle-apihttp-status-code-404google-calendar-api

解决方案


推荐阅读