【发布时间】:2019-03-11 13:34:03
【问题描述】:
更新:我解决了这个问题并将解决方案发布为下面的答案! ;)
我需要使用 Google API 创建一个活动并将其添加到 Google 日历。
目前我只知道如何从 Google 日历中获取我拥有的所有活动。这是我到目前为止所得到的:
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
namespace CalendarQuickstart
{
class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-dotnet-quickstart.json
static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
static string ApplicationName = "Google Calendar API .NET Quickstart";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// List events.
Events events = request.Execute();
Console.WriteLine("Upcoming events:");
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.DateTime.ToString();
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Console.WriteLine("{0} ({1})", eventItem.Summary, when);
}
}
else
{
Console.WriteLine("No upcoming events found.");
}
Console.Read();
}
}
}
我想做的一定是这样的:
var ev = new Event();
EventDateTime start = new EventDateTime();
start.DateTime = new DateTime(2019, 3, 11, 10, 0, 0);
EventDateTime end = new EventDateTime();
end.DateTime = new DateTime(2019, 3, 11, 10, 30, 0);
ev.Start = start;
ev.End = end;
ev.Description = "Description...";
events.Items.Insert(0, ev);
我花了一整天的时间搜索任何 .NET 示例,但一无所获。 任何帮助表示赞赏! ;)
【问题讨论】:
-
我花了 10 秒谷歌搜索才找到这个:developers.google.com/resources/api-libraries/documentation/…
-
@IanKemp 谢谢!我仍然收到一个错误:
EventsResource.InsertRequest does not contain a definition for "InsertRequest"如果你能告诉我一个如何使用它的例子,那就太好了!我也试过这个:service.Events.Insert(ev, "primary");,但我仍然没有看到我刚刚在我的日历中创建的事件。 -
这个问题我已经解决了,谢谢大家!
标签: c# .net google-api google-calendar-api