更新:我解决了这个问题,并将解决方案作为答案发布在下面! ;)
我需要创建一个事件,然后使用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示例,但一无所获。 任何帮助表示赞赏! ;)
答案 0 :(得分:1)
我已经解决了这个问题!因此,在进行项目更改之前
static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
到
static string[] Scopes = { CalendarService.Scope.Calendar };
如果您已经构建了解决方案,请删除 credentials.json 文件,然后重新加载。添加事件的代码在这里:
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.Summary = "New Event";
ev.Description = "Description...";
var calendarId = "primary";
Event recurringEvent = service.Events.Insert(ev, calendarId).Execute();
Console.WriteLine("Event created: %s\n", e.HtmlLink);
这是我第一次尝试使用Google API,所以不要判断我;)希望有一天能对您有所帮助!
注意:您还需要从\ bin \ debug文件夹中删除“ token.json”文件夹
答案 1 :(得分:0)
请查看Urls,帮助您了解更多信息
Google Calendar API, Adding an event to someones calendar just by knowing their e-mail address
代码项目参考
https://www.codeproject.com/Articles/36482/How-to-Create-Google-Calendar-Events-Using-NET
https://www.codeproject.com/Articles/565032/Google-Calendar-v-Integration-in-ASP-NET-Create-ed