Generate Google Meets Link using Service Account ASP.NET

369 views Asked by At

My requirement is to generate a Google Meet link using ASP.NET and Google Calendar API. I created a service account in Google Cloud and gave all the required credentials and permissions to the backend. I attached the below code, it generates a Google Calendar event successfully without a Google Meet link. But when I add ConferenceData attributes to the event object, I get a "Bad request" error.

This is my code without ConferenceData attributes:

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

GoogleCredential credential;

using (var stream = new FileStream(Path.Combine(Directory.GetCurrentDirectory(), "Cre", "cred.json"), FileMode.Open, FileAccess.Read))
{
    credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
}

var services = new CalendarService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = ApplicationName,
});

var @event = new Event
{
    Summary = "Tecoora Online Meeting",
    Location = "Online/Google Meet",
    Description = "Discussion with Lawyer",
    Creator = new Event.CreatorData()
    {
        DisplayName = "Tecoora Google Meet",
        Email = "[email protected]"
    },
    Organizer = new Event.OrganizerData()
    {
        DisplayName = "Tecoora Google Meet",
        Email = "[email protected]"
    },
    Start = new EventDateTime()
    {
        DateTimeDateTimeOffset = timeSlot.FromTime,
    },
    End = new EventDateTime()
    {
        DateTimeDateTimeOffset = timeSlot.ToTime,
    },
    AnyoneCanAddSelf = true,
    GuestsCanInviteOthers = true,
    GuestsCanModify = true,
    GuestsCanSeeOtherGuests = true,
    Visibility = "public",
};

var eventRequest = services.Events.Insert(@event, "primary");
if(eventRequest == null)
{
    response.Error = "Google Meet Link Generation Issue.";
    response.Success = false;
    return response;
}

eventRequest.ConferenceDataVersion = 1;
Event createdEvent = await eventRequest.ExecuteAsync();
if(createdEvent == null)
{
    response.Error = "Google Meet Link Creation Issue.";
    response.Success = false;
    return response;
}

The above code generates Google Calendar event successfully. But when I added ConferenceData like the code shown below, I get a "Bad request".

This is the event, with conference data:

var @event = new Event
{
    Summary = "Tecoora Online Meeting",
    Location = "Online/Google Meet",
    Description = "Discussion with Lawyer",
    Creator = new Event.CreatorData()
    {
        DisplayName = "Tecoora Google Meet",
        Email = "[email protected]"
    },
    Organizer = new Event.OrganizerData()
    {
        DisplayName = "Tecoora Google Meet",
        Email = "[email protected]"
    },
    Start = new EventDateTime()
    {
        DateTimeDateTimeOffset = timeSlot.FromTime,
    },
    End = new EventDateTime()
    {
        DateTimeDateTimeOffset = timeSlot.ToTime,
    },
    AnyoneCanAddSelf = true,
    GuestsCanInviteOthers = true,
    GuestsCanModify = true,
    GuestsCanSeeOtherGuests = true,
    Visibility = "public",
    ConferenceData = new ConferenceData()
    {
        CreateRequest = new CreateConferenceRequest()
        {
            RequestId = Guid.NewGuid().ToString(),
            ConferenceSolutionKey = new ConferenceSolutionKey()
            {
                Type = "hangoutsMeet"
            }
        }
    }

This is the error from the Google Calendar API:

{
    "data": null,
    "success": false,
    "error": "The service calendar has thrown an exception. HttpStatusCode is BadRequest. Invalid conference type value."
}

I changed to type also and tried - hangoutsMeet / eventNamedHangout.

This type of change also not working for me.

When I am finding a solution for this issue some Stackoverflow answers suggest enabling domain-wide delegation in my Google Workspace account. But my Google Workspace account -> Security doesn't see the domain-wide delegation option. I logged into Google Workspace as SuperAdmin. I attached a Google Workspace screenshot. enter image description here

I need to find a solution to this issue. Please help me.

1

There are 1 answers

3
Linda Lawton - DaImTo On BEST ANSWER

You need to use CreateWithUser to define the user you want the service account to impersonate on your doamin.

using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Services;

Console.WriteLine("Hello, World!");

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

const string workspaceAdmin = "[REDACTED]";

const string credentials = @"C:\\workspaceserviceaccount.json";

credential = GoogleCredential.FromFile(credentials).CreateScoped(Scopes).CreateWithUser(workspaceAdmin);

var services = new CalendarService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
});

var results = services.Calendars.Get("primary").Execute();

Console.WriteLine(results.Id);

Unlock the Power of Google Calendar in .NET with Domain-Wide Delegation!