คำขอ FreeBusy ของ Google Calendar API V3 ได้รับข้อผิดพลาดคำขอไม่ถูกต้อง

ฉันกำลังพยายามดึงข้อมูล FreeBusy จากปฏิทิน Google ของฉันโดยใช้ .NET client API V3

รหัสต่อไปนี้อนุญาตให้ฉันเข้าสู่ระบบโดยใช้การรับรองความถูกต้องของบริการและดึงรายการปฏิทินและการตั้งค่าปฏิทิน ฉันยังสามารถดึงข้อมูลเหตุการณ์ตามที่แสดง

เพื่อให้งานนี้สำเร็จ ฉันได้แชร์ปฏิทินกับรหัสการพัฒนาของฉัน:

‹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);
        }
    }
}

อย่างไรก็ตาม รหัสที่พยายามดึงข้อมูลว่างว่างจะได้รับข้อผิดพลาด "คำขอไม่ถูกต้อง" เสมอ

ฉันได้ตรวจสอบคำขอที่ส่งโดยใช้พู้ทำเล่นแล้ว และเท่าที่ฉันเห็นมันถูกต้อง

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":"[email protected]"}],"timeMax":"2013-01-301T00:00:00.000Z","timeMin":"2013-01-101T00:00:00.000Z","timeZone":"Europe/London"}

ใครช่วยบอกฉันได้ไหมว่าทำไมฉันถึงได้รับข้อผิดพลาดคำขอที่ไม่ถูกต้อง และที่สำคัญกว่านั้นคือจะแก้ไขได้อย่างไร

ขอบคุณมาก

คริส.


person C Hall    schedule 10.02.2013    source แหล่งที่มา
comment
ฉันมองเห็นความผิดพลาดของตัวเองในที่สุด ตรวจสอบวันที่ในคำขอ timeMin 2013-01-101 timeMax 2013-01-301 การเปลี่ยนสิ่งเหล่านี้เป็นวันที่ที่ถูกต้องได้แก้ไขแล้ว หวังว่าเมื่อพิจารณาถึงระยะเวลาที่ฉันต้องใช้ในการหาวิธีทำให้การรับรองความถูกต้องของบริการทำงานได้ รหัสนี้จะเป็นประโยชน์กับใครบางคน คริส   -  person C Hall    schedule 10.02.2013


คำตอบ (1)


คำตอบอยู่ในความเห็นข้างบนครับ ผมจำวันที่ผิดครับ

person C Hall    schedule 17.10.2014