我正在使用谷歌日历API,检索指定日历的所有事件。在设置了我的凭据并调用以下命令后,获得我所有日历的列表:
var test = service.Calendars.Get(id).Execute();
Execite()正在对google服务器进行调用,但我没有从该调用中得到响应。它一直处于空闲状态
public static ServiceAccountCredential Authenticate(){
string serviceAccountEmail = "...@developer.gserviceaccount.com";
string keyFilePath = @"filepath";
// what googleApi features to access
_scopes = new string[]
{
CalendarService.Scope.Calendar, // Manage your calendars
CalendarService.Scope.CalendarReadonly // View your Calendars
};
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = _scopes
}.FromCertificate(certificate));
return credential;
}
public class GoogleCalenderRepository
{
ServiceAccountCredential _credentials;
public GoogleCalenderRepository()
{
_credentials = GoogleServiceAccountAuthentication.Authenticate();
}
// use async and await
public string RoomAvailability()
{
CalendarService service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = _credentials,
ApplicationName = "My Project",
});
var calender = service.Calendars.Get("@...group.calendar.google.com").Execute();
return null;
}
}
编辑:当调用Execute()在我的输出屏幕,我得到:
线程0x13f4已退出,代码为0 (0x0)
这个例子是用VB编写的。
它向您展示了如何调用Google Calendar API来获取用户名的日历列表。您可以手动或使用在线服务之一将其转换为c#。
Dim objCalendarListRequest As CalendarListResource.ListRequest
Dim objCalendars As Data.CalendarList
objCalendarListRequest = objCalendarService.CalendarList.List
objCalendars = objCalendarListRequest.Execute()
For Each objCal As Data.CalendarListEntry In objCalendars.Items
If objCal.Primary IsNot Nothing AndAlso objCal.Primary Then
Debug.WriteLine("ID=" & objCal.Id & ", Description=" & objCal.Description & ", Summary=" & objCal.Summary)
Exit For
End If
Next