【问题标题】:Google Calendar API Create/Get Events - ASP.NET C#Google 日历 API 创建/获取事件 - ASP.NET C#
【发布时间】:2017-04-05 16:48:02
【问题描述】:

我想访问曾经登录过我的应用程序的用户的日历活动 使用谷歌誓言,也应该能够从我的应用程序创建事件。如何使用 ASP.NET C# 做到这一点?

我可以使用我在此期间创建的客户端 ID/密钥来访问我的日历活动 日历API项目创建流程如下:

https://console.developers.google.com/flows/enableapi?apiid=calendar&pli=1

感谢任何帮助或示例代码。

这是我使用的代码:

 

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(

    new ClientSecrets
     {
                    ClientId = "Client-Id",
                    ClientSecret = "Client-Secret",
                },
                new[] { CalendarService.Scope.CalendarReadonly },
                "user",
                CancellationToken.None).Result;

       

            // Create the service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            CalendarListResource.ListRequest cal = service.CalendarList.List();
            cal.MaxResults = 100;
            
               var calresult = cal.Execute().Items;
            
            
            List<EventDetails> ed = new List<EventDetails>();
  
                foreach (CalendarListEntry entry in calresult)
                {
                   EventsResource.ListRequest request = service.Events.List(entry.Id);
                        request.TimeMin = Convert.ToDateTime("03/01/2015");
                        request.ShowDeleted = false;
                        request.SingleEvents = true;
                        request.MaxResults = 200;
                        request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;

                        // List events.
                        Events events = request.Execute();
                }

谢谢。

【问题讨论】:

  • 通常情况下,人们在这里发布一个明确定义的问题。 “这是抛出这个错误”,“当我尝试 X 时它会执行 Y,我需要它来执行 Z”。看起来你写了一些代码,那它有什么问题?
  • 此代码适用于我的谷歌帐户,它为我的日历提取事件,但是当我使用其他电子邮件地址时,它给出了权限被拒绝错误。
  • 另一个问题是我可以使用我的帐户在本地计算机上使用 oauth 访问日历事件,但是当将相同的代码部署到服务器时会出现此错误。 “System.AggregateException:发生一个或多个错误。- System.UnauthorizedAccessException:对路径 'Google.Apis.Auth' 的访问被拒绝”

标签: c# asp.net google-api google-calendar-api google-api-dotnet-client


【解决方案1】:

授权:

当您运行代码时,将弹出以下代码并请求用户验证您的应用程序。

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(

new ClientSecrets
 {
                ClientId = "Client-Id",
                ClientSecret = "Client-Secret",
            },
            new[] { CalendarService.Scope.CalendarReadonly },
            "user",
            CancellationToken.None).Result;

这里的关键是“用户”,默认情况下,该库将凭据存储在您机器上的 %appdata% 目录中。您希望访问其数据的每个用户都必须授予您对其帐户的访问权限。

评论错误:当我使用其他电子邮件地址时,它给出了权限被拒绝错误。

我不确定您在哪里提供此其他电子邮件地址,但请确保您已更改代码中的“用户”,以确保您已通过您希望访问的帐户的用户进行身份验证

评论错误访问路径“Google.Apis.Auth”被拒绝”

默认情况下,filedatastore 将凭据存储在 %appData% 文件夹中,听起来您的服务器无权访问该文件夹。我有一篇文章解释了如何更改它FileDatastore demystified

范围:

Scope 表示您想要的访问类型 CalendarService.Scope.CalendarReadonly 只会为您提供对用户帐户的只读访问权限

注意:我不确定访问日历与访问用户日历是什么意思。如果您想让他们访问您的日历,您应该使用服务帐户。

【讨论】:

  • 嗨,你能帮我解决这个问题吗?我在下面发布了详细信息。谢谢。
  • 使用以下代码,但服务器无响应。使用 (var stream = new FileStream(clientSecretsJsonFilePath, FileMode.Open, FileAccess.Read)) { credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath,真))。结果; }
  • 尝试为 Filedatestore 添加 Drive.Api.Auth.Store,出现以下错误“拒绝访问路径 'Drive.Api.Auth.Store'。”
  • 我会假设 IIS 无权访问该路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-24
相关资源
最近更新 更多