0

使用服务帐户,如何通过使用 google 日历 API 创建事件来生成 google meet 链接。我的授权一切正常。但我不知道是什么。这是我的代码,成功创建事件但未生成 google meet 链接。响应显示事件创建详细信息,但没有关于 google-meet。我真的很感激一些帮助。

        string calendarId = @"calendar-id";

        string[] Scopes = { CalendarService.Scope.Calendar };

        ServiceAccountCredential credential;
        string path = Server.MapPath("~/file.json");
        using (var stream =
            new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            var confg = Google.Apis.Json.NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(stream);
            credential = new ServiceAccountCredential(
               new ServiceAccountCredential.Initializer(confg.ClientEmail)
               {
                   Scopes = Scopes
               }.FromPrivateKey(confg.PrivateKey));
        }


        var service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Calendar API Sample",
        });

        var calendar = service.Calendars.Get(calendarId).Execute();
        string token = credential.Token.AccessToken;
        // Define parameters of request.
        EventsResource.ListRequest listRequest = service.Events.List(calendarId);
        listRequest.TimeMin = DateTime.Now;
        listRequest.ShowDeleted = false;
        listRequest.SingleEvents = true;
        listRequest.MaxResults = 10;
        listRequest.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;

        Event newEvent = new Event();
        {
            DateTime start = Convert.ToDateTime("2021-06-18T05:00:02.000Z");
            DateTime end = Convert.ToDateTime("2021-06-18T06:00:02.000Z");
            newEvent.Summary = "summary".ToString();
            newEvent.Description = "description".ToString();
            newEvent.Status = "confirmed";
            newEvent.Creator = new Event.CreatorData
            {
                Email = " email-id",
                Self = true
            };
            newEvent.Organizer = new Event.OrganizerData
            {
                Email = "email-id",
                Self = true
            };
            newEvent.Start = new EventDateTime
            {
                DateTime = start,
                TimeZone = "Asia/Kolkata"

            };
            newEvent.End = new EventDateTime
            {
                DateTime = end,
                TimeZone = "Asia/Kolkata"
            };
            newEvent.HangoutLink = "";

            newEvent.ConferenceData = new ConferenceData()
            {
                ConferenceSolution = new ConferenceSolution
                {
                    Key = new ConferenceSolutionKey
                    {
                        Type = "hangoutsMeet"
                    }

                },
                CreateRequest = new CreateConferenceRequest()
                {

                    ConferenceSolutionKey = new ConferenceSolutionKey()
                    {
                        Type = "hangoutsMeet"
                    },
                    RequestId = "some-random-string"
                },


            };
            //newEvent.Attendees = new List<EventAttendee>()
            //{
            //    new EventAttendee() { Email = "" }
            //};
        };

        RestClient restClient = new RestClient();
        RestRequest request = new RestRequest();

        var serilaizeJson = JsonConvert.SerializeObject(newEvent, Formatting.None,
        new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore
        });

        request.AddQueryParameter("key", "api-key");
        request.AddHeader("Authorization", "Bearer " + token);
        request.AddHeader("Accept", "application/json");
        request.AddHeader("Content-Type", "application/json");
        request.AddParameter("application/json", serilaizeJson, ParameterType.RequestBody);

        restClient.BaseUrl = new System.Uri("https://www.googleapis.com/calendar/v3/calendars/calendar-id/events?conferenceDataVersion=1");
        var response = restClient.Post(request);
        if (response.StatusCode == System.Net.HttpStatusCode.OK)
        {
            dynamic returnFile = JObject.Parse(response.Content);
            string link = returnFile["hangoutLink"];
        }
4

1 回答 1

0

解决方案

将以下对象添加到请求正文

conferenceData: {
  createRequest: {
    requestId: "sample123",
    conferenceSolutionKey: { type: "hangoutsMeet" },
  },
}
于 2021-06-18T09:34:16.870 回答