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

pod7payv  于 12个月前  发布在  .NET
关注(0)|答案(1)|浏览(235)

我的要求是使用ASP.NET和Google Calendar API生成一个Google Meet链接。我在Google Cloud中创建了一个服务帐户,并向后端提供了所有必需的凭据和权限。我附上了下面的代码,它成功地生成了一个Google Calendar事件,而没有Google Meet链接。但是当我向事件对象添加ConferenceData属性时,我得到了一个“Bad request”错误。
这是我没有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时,我得到了一个“Bad request”。
这是事件,与会议数据:

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日历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屏幕截图。x1c 0d1x
我需要找到解决这个问题的方法。请帮助我。

70gysomp

70gysomp1#

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

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!

相关问题