Google日历API的Google服务对象 [英] Google service object for Google Calendar API

查看:137
本文介绍了Google日历API的Google服务对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在.NET中使用Google Calendar API,特别是我试图获取事件列表。根据此处的示例,使用不同的编程语言需要创建一个'服务'对象和一个'事件'对象。但是,我无法找到这些对象之一或者如何启动它们的明确解释。有没有人有解释?或者任何人都可以提供任何信息或给我一个链接到这里解释的地方?它不一定必须在.NET中。



以下是Java中的示例:

  String pageToken = null; 
do {
events = service.events()。list('primary')。setPageToken(pageToken).execute();
列表< Event> items = events.getItems();
for(Event event:items){
System.out.println(event.getSummary());
}
pageToken = events.getNextPageToken();

} while(pageToken!= null);

遵循建议回答,我收到以下错误:

无法加载文件或程序集'Microsoft.Threading.Tasks.Extensions.Desktop,Version = 1.0.16.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'或其某个依赖项。系统无法找到指定的文件。



以下是代码,错误发生在 credentials = Await上。 ..

  Dim凭证UserCredential 
Dim clientSecretsPath As String = Server.MapPath (〜/ App_Data / client_secret.json)
Dim作为IList(Of String)=新列表(字符串)()
scopes.Add(CalendarService.Scope.Calendar)

使用stream = New System.IO.FileStream(clientSecretsPath,System.IO.FileMode.Open,System.IO.FileAccess.Read)
credential =等待GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream))。秘密,范围,用户,CancellationToken.None)
结束使用


解决方案

请记住,此示例适用于Java。我的建议是执行以下操作:


  1. 查看我们的VB示例中可用的Calendar API,其中 here

  2. 您还应该在其他示例中查看C#,比如任务API示例

  3. 开始一个新项目并添加一个NuGet引用到 Google.Apis.Calednar.v3 。请记住它是预发布版本。

  4. 您的代码应该如下所示:

在上面的两个示例中,我没有编译或测试它,但它应该可以工作。

  UserCredential凭证; 
using(var stream = new System.IO.FileStream(client_secrets.json,
System.IO.FileMode.Open,System.IO.FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new [] {CalendarService.Scope.Calendar},
user,CancellationToken.None);
}

//创建服务。
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer =凭证,
ApplicationName =您的应用程序名称在这里,
});

var firstCalendar =(等待service.CalendarList.List().executeAsync())。Items()。FirstOrDefault();
if(firstCalendar!= null)
{
//获取第一个日历中的所有事件。
var calEvents = await service.Events.List(firstCalendar.Id).ExecuteAsync();
//做某事
var nextPage = calEvents.NextPage;
while(nextPage!= null)
{
var listRequest = service.Events.List(firstCalendar.Id);
//设置页面令牌以获取下一个事件。
listRequest.PageToken = nextPage;
calEvents =等待listRequest.EsecuteAsync();
//做某事
nextPage = calEvents.NextPage;
}
}


I am trying to use the Google Calendar API in .NET, specifically I am trying to get a list of events. According to the examples here, in different programming languages I need to create a 'service' object and an 'event' object. However, I can't find a clear explanation of what either of these objects is or how to initiate them. Does anyone have an explanation? Or can anyone provide any information or give me a link to where this is explained? It doesn't necessarily have to be in .NET

Here is the example in Java:

String pageToken = null;
do {
   events = service.events().list('primary').setPageToken(pageToken).execute();
List<Event> items = events.getItems();
for (Event event : items) {
  System.out.println(event.getSummary());
}
pageToken = events.getNextPageToken();

} while (pageToken != null); 

Following the advice answered, I am getting the following error:

Could not load file or assembly 'Microsoft.Threading.Tasks.Extensions.Desktop, Version=1.0.16.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

Here is the code, the error occurs on the credentials = Await... line

Dim credential As UserCredential
Dim clientSecretsPath As String = Server.MapPath("~/App_Data/client_secret.json")
Dim scopes As IList(Of String) = New List(Of String)()
    scopes.Add(CalendarService.Scope.Calendar)

Using stream = New System.IO.FileStream(clientSecretsPath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
credential = Await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, scopes, "user", CancellationToken.None)
    End Using

解决方案

Remember that this sample is for Java. My recommendation is to do the following:

  1. Take a look in our VB sample for the Calendar API which is available here
  2. You should take a look also in other sample for C#, let's say Tasks API sample
  3. Start a new project and add a NuGet reference to Google.Apis.Calednar.v3. Remember that it's prerelease version.
  4. Your code should look like the following:

It's based on the 2 samples above, I didn't compile or test it but it should work.

  UserCredential credential;
  using (var stream = new System.IO.FileStream("client_secrets.json",
  System.IO.FileMode.Open, System.IO.FileAccess.Read))
  {
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
      GoogleClientSecrets.Load(stream).Secrets, 
      new[] { CalendarService.Scope.Calendar }, 
      "user", CancellationToken.None);
  }

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

  var firstCalendar = (await service.CalendarList.List().ExecuteAsync()).Items().FirstOrDefault();
  if (firstCalendar != null)
  {
     // Get all events from the first calendar.
     var calEvents = await service.Events.List(firstCalendar.Id).ExecuteAsync();
     // DO SOMETHING
     var nextPage = calEvents.NextPage;
     while (nextPage != null)
     {
       var listRequest = service.Events.List(firstCalendar.Id);
       // Set the page token for getting the next events.
       listRequest.PageToken = nextPage;
       calEvents = await listRequest.EsecuteAsync();
       // DO SOMETHING
       nextPage = calEvents.NextPage;
     }
  }

这篇关于Google日历API的Google服务对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆