Google Calendar API V3 FreeBusy请求收到错误的请求错误 [英] Google Calendar API V3 FreeBusy request receives Bad Request Error

查看:67
本文介绍了Google Calendar API V3 FreeBusy请求收到错误的请求错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.NET客户端API V3从Google日历中获取FreeBusy数据.

I'm trying to fetch FreeBusy data from my Google calendar using the .NET client API V3.

以下代码允许我使用服务身份验证登录并获取日历列表和日历设置.我还可以如图所示获取事件.

The following code allows me to login using a service authentication and fetch the calendar list and calendar settings. I can also fetch the events as shown.

为完成这项工作,我与开发ID共享了日历:

To make this work I have shared my calendar with my development Id:

< my-id> @ developer.gserviceaccount.com

<my-id>@developer.gserviceaccount.com

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.Apis.Calendar;
using Google.Apis.Calendar.v3;
using Google.Apis.Authentication;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using DotNetOpenAuth.OAuth2;
using System.Diagnostics;
using Google.Apis.Calendar.v3.Data;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Discovery;
using Google.Apis.Drive.v2;
using Google.Apis.Util;

namespace consoleGoogleResearch
{
    class Program
    {
        private const string SERVICE_ACCOUNT_EMAIL = "<your-value>@developer.gserviceaccount.com";
        private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"<path-to\<your-value>-privatekey.p12";

        /// <summary>
        /// Build a Calendar service object authorized with the service account.
        /// </summary>
        /// <returns>Drive service object.</returns>
        static CalendarService BuildCalendarService()
        {
            AssertionFlowClient client = new AssertionFlowClient(
                GoogleAuthenticationServer.Description, new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable))
            {
                Scope = CalendarService.Scopes.Calendar.GetStringValue(),
                ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
            };
            OAuth2Authenticator<AssertionFlowClient> authenticator = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);
            return new CalendarService(authenticator);
        }

        public static void Main(string[] args)
        {
            var service = BuildCalendarService();

            // Get the calendar service
            Google.Apis.Calendar.v3.CalendarListResource.ListRequest clrq = service.CalendarList.List();
            var result = clrq.Fetch();

            // Prove we can get the settings.
            SettingsResource.ListRequest slr = service.Settings.List();
            var sr = slr.Fetch();

            try
            {
                Console.WriteLine("Calendars: ");
                foreach (CalendarListEntry calendar in result.Items)
                {
                    Console.WriteLine("{0}", calendar.Id);
                    Console.WriteLine("\tAppointments:");
                    Google.Apis.Calendar.v3.EventsResource.ListRequest elr = service.Events.List(calendar.Id);
                    var events = elr.Fetch();
                    if (events.Items != null)
                    {
                        foreach (Event e in events.Items)
                        {
                            Console.WriteLine("\tSummary: {0}, Location: {1}", e.Summary, e.Location);
                            if (e.IsAllDayEvent())
                            {
                                Console.WriteLine("\t\tAll Day: {0}", e.Start.GetDateTime().ToLongDateString());
                            }
                            else
                            {
                                Console.WriteLine("\t\tFrom: {0}", e.Start.GetDateTime());
                                Console.WriteLine("\t\tTo: {0}", e.End.GetDateTime());
                            }
                            if (e.Attendees != null)
                            {
                                foreach (var att in e.Attendees)
                                {
                                    Console.WriteLine("Attendee:\t\t\t{0}<{1}>", att.DisplayName, att.Email);
                                }
                            }
                            Console.WriteLine();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.DebugFormat("Error: {0}", ex.Message);
            }

            // Attempt to get free busy data.
            FreebusyResource.QueryRequest fbq = service.Freebusy.Query(new FreeBusyRequest());

            FreeBusyRequestItem c = new FreeBusyRequestItem();
            c.Id = "<your calendar id>";
            fbq.Body.Items = new List<FreeBusyRequestItem>();
            fbq.Body.Items.Add(c);
            fbq.Body.TimeZone = "Europe/London";
            fbq.Body.TimeMin = "2013-01-101T00:00:00.000Z";
            fbq.Body.TimeMax = "2013-01-301T00:00:00.000Z";

            // This call fails with global bad request
            var fbres = fbq.Fetch();

            Console.ReadKey();
        }
    }

    static internal class Extensions
    {
        static internal DateTime GetDateTime(this EventDateTime edt)
        {
            if (String.IsNullOrEmpty(edt.DateTime))
            {
                if (String.IsNullOrEmpty(edt.Date))
                {
                    return DateTime.MinValue;
                }
                else
                {
                    return DateTime.Parse(edt.Date);
                }
            }
            else
            {
                return DateTime.Parse(edt.DateTime);
            }
        }

        static internal Boolean IsAllDayEvent(this Event e)
        {
            return (e.Start.DateTime == null && e.Start.Date != null);
        }
    }
}

但是,尝试获取空闲繁忙信息的代码始终会收到错误请求"错误.

However the code that attempts to fetch free busy information always receives a "bad request" error.

我已经检查了使用提琴手发送的请求,据我所知它是正确的.

I have checked the request sent using fiddler and as far as I can see it is correct.

POST https://www.googleapis.com/calendar/v3/freeBusy?alt=json&prettyPrint=true HTTP/1.1
Authorization: Bearer <removed>
Content-Type: application/json; charset=utf-8
User-Agent: ConsoleApplication1 google-api-dotnet-client/ Win32NT/6.1.7601.65536 (gzip)
Host: www.googleapis.com
Content-Length: 144
Accept-Encoding: gzip, deflate

{"items":[{"id":"apps.vivid@gmail.com"}],"timeMax":"2013-01-301T00:00:00.000Z","timeMin":"2013-01-101T00:00:00.000Z","timeZone":"Europe/London"}

谁能告诉我为什么我收到错误的请求错误,更重要的是如何解决它?

Can anyone tell me why I'm receiving the bad request error and more importantly how to fix it?

非常感谢

克里斯.

推荐答案

答案在上面的注释中,我在日期上弄错了.

The answer is in the comment above, I made a mistake with the dates.

这篇关于Google Calendar API V3 FreeBusy请求收到错误的请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆