带有ASP.NET的Google Calendar API [英] Google Calendar API with ASP.NET
问题描述
对于使用Google Calendar API在ASP.NET网络表单(C#)中添加/修改事件感到困惑.
I'm confused about using the Google Calendar API for adding/modifying events in ASP.NET webforms (C#).
我不确定是否需要oAuth或什么.我的应用程序在我自己的服务器上,用于访问我自己的域和日历.我不需要其他用户给我访问他们的日历的权限;我只需要通过我的应用程序访问自己的内容即可.
I'm not sure if I need oAuth or what. My app is on my own server accessing my own domain and my own calendar. I don't need other users to give me access to their calendar; I only need to access my own via my app.
在我的aspx页面之一上,我想将事件信息发送到我的Google日历以添加(或以后修改)该事件.
On on one of my aspx pages, I'd like to send event info to my Google calendar to add (or later modify) the event.
我检查了各种代码示例和Google入门指南.我只是不清楚确切需要什么.我已经设置了API密钥和oAuth2客户端ID. Google的说明使我发了圈,这很可能是由于我需要澄清所需要的内容.
I've checked all kinds of code examples and the Google getting started guides. I'm just not clear on what exactly is needed. I've set up an API key and an oAuth2 client ID. The Google instructions have sent me in circles and it's likely due to my needing clarification on what's need.
有人可以消除我的困惑并指出正确的方向吗?
Can someone please clear-up my confusion and point me in the right direction?
推荐答案
摘要:
-
呼叫Google clood oauth2受保护的资源
Summary :
To Call a google clould oauth2 protected resource
从您的服务器到Google服务器
From your server to google server
没有用户交互
访问您自己的数据
使用C#
代码:
var private_key = @"-----BEGIN PRIVATE KEY-ccc-END PRIVATE KEY-----\n"; string calendarId = @"xxxxxxxxxxxxx@group.calendar.google.com"; var client_email = @"my-google-calender@xxx.iam.gserviceaccount.com"; var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(client_email) { Scopes = new string[] { CalendarService.Scope.Calendar } }.FromPrivateKey(private_key)); var service = new CalendarService(new BaseClientService.Initializer() { HttpClientInitializer = credential, });
-
使用 service 方法获取数据
Use service methods to get the data
生成私钥和client_email. "noreferrer>此链接
Private Key and client_email can be generated from this link
日历ID可以在calendar.google.com
Calendar Id can be found on calendar.google.com
您必须与client_email共享您的日历查看演示
You must share your calendar with client_email see the demo
Google You You Pay + Pay + Pay + Google Google You Manage Manage Manage% +----------+ +----------+ +----------+ | Gmail | | | | | | Calendar | | G Suite | | Google | | drive | | | | Cloud | | | | | | | +----^-----+ +----+-----+ +------+---+ | ^ ^ | | | | | | | | | +-------------------------------------------------------------+ | | | | | | | | | | | | | Google | | | | | Oauth2 | | | | | Server | | | | | | | | | | | | +-------------------------------------------------------------+ | | | | | +----------------+ | | | | | | | | | | | No | |require | | | Consent | |admin | | | | |consent | | | |require | | +-------+ |user | | | |consent +---------+ Your app | | | | | | | | | | | | | +-------------------------+ | | | | | | | +----------------+ You Pay + You Manage
分步演示
步骤01:打开Google控制台
https://console.developers.google. com/projectselector/apis/library/calendar-json.googleapis.com
Step by Step demo
Step 01 : open google console
https://console.developers.google.com/projectselector/apis/library/calendar-json.googleapis.com
- calendar.google.com
- 为您之前在步骤09 中复制的服务帐户添加电子邮件
- 也更改权限进行更改并管理共享
-
点击发送
- Add the email for the service account that you copied before in step 09
- change the Permissions too Make changes and manage sharing
click send
步骤14:在同一页上复制并保存日历ID ,我们将需要它
Step 14: on the same page copy and save the Calendar ID we will need it
Install-Package Google.Apis.Calendar.v3
using Google.Apis.Auth.OAuth2; using Google.Apis.Calendar.v3; using Google.Apis.Calendar.v3.Data; using Google.Apis.Services; using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace CalendarQuickstart { class Program { static void Main(string[] args) { string jsonFile = "xxxxxxx-xxxxxxxxxxxxx.json"; string calendarId = @"xxxxxxxxxxxxx@group.calendar.google.com"; string[] Scopes = { CalendarService.Scope.Calendar }; ServiceAccountCredential credential; using (var stream = new FileStream(jsonFile, FileMode.Open, FileAccess.Read)) { var confg = Google.Apis.Json.NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(stream); credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(confg.ClientEmail) { Scopes = Scopes }.FromPrivateKey(confg.PrivateKey)); } var service = new CalendarService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Calendar API Sample", }); var calendar = service.Calendars.Get(calendarId).Execute(); Console.WriteLine("Calendar Name :"); Console.WriteLine(calendar.Summary); Console.WriteLine("click for more .. "); Console.Read(); // Define parameters of request. EventsResource.ListRequest listRequest = service.Events.List(calendarId); listRequest.TimeMin = DateTime.Now; listRequest.ShowDeleted = false; listRequest.SingleEvents = true; listRequest.MaxResults = 10; listRequest.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime; // List events. Events events = listRequest.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.WriteLine("click for more .. "); Console.Read(); var myevent = DB.Find(x => x.Id == "eventid" + 1); var InsertRequest = service.Events.Insert(myevent, calendarId); try { InsertRequest.Execute(); } catch (Exception) { try { service.Events.Update(myevent, calendarId, myevent.Id).Execute(); Console.WriteLine("Insert/Update new Event "); Console.Read(); } catch (Exception) { Console.WriteLine("can't Insert/Update new Event "); } } } static List<Event> DB = new List<Event>() { new Event(){ Id = "eventid" + 1, Summary = "Google I/O 2015", Location = "800 Howard St., San Francisco, CA 94103", Description = "A chance to hear more about Google's developer products.", Start = new EventDateTime() { DateTime = new DateTime(2019, 01, 13, 15, 30, 0), TimeZone = "America/Los_Angeles", }, End = new EventDateTime() { DateTime = new DateTime(2019, 01, 14, 15, 30, 0), TimeZone = "America/Los_Angeles", }, Recurrence = new List<string> { "RRULE:FREQ=DAILY;COUNT=2" }, Attendees = new List<EventAttendee> { new EventAttendee() { Email = "lpage@example.com"}, new EventAttendee() { Email = "sbrin@example.com"} } } }; } }
第22步:用您的json文件名替换json文件名
string jsonFile = "xxxxxxx-xxxxxxxx.json";
步骤23:将日历ID替换为步骤14中的calendarId
string calendarId = @"xxxxxxxxxxxxx@group.calendar.google.com";
第24步:运行应用
2019/01/13
这篇关于带有ASP.NET的Google Calendar API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-