使用服务帐户 ASP.NET 生成 Google Meets 链接

问题描述 投票:0回答:1

我的要求是使用 ASP.NET 和 Google Calendar API 生成 Google Meet 链接。我在 Google Cloud 中创建了一个服务帐户,并向后端提供了所有必需的凭据和权限。我附上了以下代码,它成功生成了 Google 日历活动,而无需 Google Meet 链接。但是当我向事件对象添加

ConferenceData
属性时,我收到“错误请求”错误。

这是我的代码,没有

ConferenceData
属性:

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

上面的代码成功生成了Google日历事件。但是当我像下面所示的代码一样添加

ConferenceData
时,我收到“错误请求”。

这是活动,附有会议数据:

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"
            }
        }
    }

这是来自 Google Calendar API 的错误:

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

我也更改为键入并尝试 -hangoutsMeet / eventNamedHangout。

这种类型的改变对我来说也不起作用。

当我寻找此问题的解决方案时,一些 Stackoverflow 答案建议在我的 Google Workspace 帐号中启用全域委派。但我的 Google Workspace 帐户 -> 安全性没有看到全域委派选项。我以超级管理员身份登录 Google Workspace。我附上了 Google Workspace 屏幕截图。

Screenshot of Google Workspace Account

我需要找到解决这个问题的方法。请帮助我。

asp.net google-calendar-api google-meet
1个回答
0
投票

您需要使用 CreateWithUser 来定义您希望服务帐户在您的域上模拟的用户。

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);
© www.soinside.com 2019 - 2024. All rights reserved.