0

Using a service account, how to generate a google meet link by creating an event using google calendar API. I have my authorization all working fine. But I don't have any idea what. Here is my code, creating events successfully but not generating a google meet link. The response shows event creation details but nothing about google-meet. I'd really appreciate some help.

        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"];
        }
Ajay P Manoj
  • 1
  • 1
  • 2
  • Can you please confirm whether you are impersonating a regular account? I cannot find that in your code. There are some limitations to service accounts creating events, that could be the reason for this issue. Can you try creating an event with attendees and check if the event is successfully created and includes these attendees? – Iamblichus Jun 23 '21 at 10:34

3 Answers3

0

Solution

Add the following object to the request body

conferenceData: {
  createRequest: {
    requestId: "sample123",
    conferenceSolutionKey: { type: "hangoutsMeet" },
  },
}
fullfine
  • 1,371
  • 1
  • 4
  • 11
  • 1
    I already added these objects, but the response only creates an event not generating google meet link. Please check the attached code above. – Ajay P Manoj Jun 18 '21 at 11:01
  • @AjayPManoj - Was there a resolution to this problem? I am facing exactly the same issue. – Ramanathan Jul 27 '22 at 16:07
0

string jsonFile = "xx.json"; string calendarId = @"xx@group.calendar.google.com";

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

        ServiceAccountCredential credential;

        using (var stream =
            new FileStream(jsonFile, 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",
        });

        Event newEvent = new Event()
        {
            Summary = "Test Event",
            Location = "Online",
            Start = new EventDateTime()
            {
                DateTime = DateTime.Now.AddHours(1),
                TimeZone = "Asia/Colombo",
            },
            End = new EventDateTime()
            {
                DateTime = DateTime.Now.AddHours(2),
                TimeZone = "Asia/Colombo",
            },
            ConferenceData = new ConferenceData()
            {
                ConferenceSolution = new ConferenceSolution()
                {
                    Key = new ConferenceSolutionKey()
                    {
                        Type = "hangoutsMeet",
                    },
                },
            },
        };

        // Insert the event and get the event ID and Hangouts Meet link
        EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
        request.ConferenceDataVersion = 1;
        Event createdEvent = request.Execute();

This Code also creating Event but not creating Google meet video conferance

0

I think the problem is that you're not allowed to create Google Meet using Service account

(it's possible that there's a way around that if you set up domain wide delegation to a specific user account)

piotr
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 14 '23 at 01:19