【发布时间】: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