You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Azure Function创建谷歌日历事件遇问题求助

Azure Function部署后无法创建谷歌日历事件问题
  • 使用Visual Studio 2022 + C#开发Azure Function,核心功能为通过谷歌日历API创建日历事件
  • 已按谷歌官方指南配置Azure工作负载身份联合,创建服务账户并关联至目标函数
  • 相同逻辑在本地普通VS应用中可正常运行,确认日历权限配置无问题
  • 部署至Azure Function后无任何预期效果,发布流程无报错,但添加的日志无法在Azure门户中查看

相关代码

using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;

namespace setEvent
{
    public class babam
    {
        private readonly ILogger _logger;

        public babam(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<babam>();
        }

        [Function("babam")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request.");
            try
            {
                _logger.LogInformation("获取服务账户凭据。");
                // 初始化服务账户凭据
                GoogleCredential credential = GoogleCredential.GetApplicationDefault()
                .CreateScoped(CalendarService.Scope.Calendar);

                // 创建日历API服务实例
                _logger.LogInformation("创建日历API服务实例。");
                var service = new CalendarService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Calendar API .NET Quickstart"
                });
                string calendarId = "36e41687bff738ba3c57426d98d17a37e604fa99485a38d1c18cc2f1e8225114@group.calendar.google.com";
                var newEvent = new Event
                {
                    Summary = "New event",
                    Location = "Haifa",
                    Start = new EventDateTime()
                    {
                        DateTime = DateTime.Parse("2024-04-30T09:00:00+02:00"),
                        TimeZone = "Asia/Jerusalem",
                    },
                    End = new EventDateTime()
                    {
                        DateTime = DateTime.Parse("2024-04-30T10:00:00+02:00"),
                        TimeZone = "Asia/Jerusalem",
                    },
                };

                // 执行日历事件创建请求
                var createdEvent = service.Events.Insert(newEvent, calendarId).Execute();
                _logger.LogInformation("事件创建成功。");
            }
            catch (Exception e)
            {
                _logger.LogError(e, "创建事件时发生错误。");
            }
            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

            response.WriteString("Welcome to Azure Functions!!!");

            return response;
        }
    }
}

内容的提问来源于stack exchange,提问作者DenVug

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 08:31:09