概括 :
Code :
var private_key = @"-----BEGIN PRIVATE KEY-ccc-END PRIVATE KEY-----\n";
string calendarId = @"[email protected] /cdn-cgi/l/email-protection";
var client_email = @"[email protected] /cdn-cgi/l/email-protection";
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,
});
Use service获取数据的方法
私钥和 client_email 可以从以下位置生成这个链接 https://console.developers.google.com/projectselector/iam-admin/serviceaccounts/create?supportedpurview=project
日历 ID 可以在 calendar.google.com 上找到
您必须与 client_email 共享您的日历看演示
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:打开谷歌控制台
https://console.developers.google.com/projectselector/apis/library/calendar-json.googleapis.com https://console.developers.google.com/projectselector/apis/library/calendar-json.googleapis.com
步骤02:点击选择
步骤03:选择或创建一个新项目
步骤04:点击启用或管理
步骤05:点击凭证
步骤06:创建服务帐户密钥
步骤07:输入服务帐户名称点击创建
步骤08:点击创建无角色然后将下载的json私钥保存在安全的地方
第 09 步:复制您的 client_email
第10步:打开谷歌日历
第 11 步:打开日历设置和共享
第12步:必须与特定人员分享然后点击添加
Step 13:
- 添加您之前复制的服务帐户的电子邮件step 09
- 也更改权限进行更改并管理共享
-
单击发送
第14步:在同一页面上复制并保存日历ID我们会需要它
第 15 步:创建新的控制台应用程序
第16步:将私钥json文件添加到您的项目中
第 17 步:右键单击私钥 json,然后单击“属性”
步骤 18:将“复制到输出目录”更改为“始终复制”
第 19 步:打开 PM Console 并在默认项目 D 上选择您的项目
第20步:安装Google.Apis日历包
Install-Package Google.Apis.Calendar.v3
第21步:用代码替换Program.cs
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 = @"[email protected] /cdn-cgi/l/email-protection";
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 = "[email protected] /cdn-cgi/l/email-protection"},
new EventAttendee() { Email = "[email protected] /cdn-cgi/l/email-protection"}
}
}
};
}
}
步骤 22:将 json 文件名替换为您的 json 文件名
string jsonFile = "xxxxxxx-xxxxxxxx.json";
步骤 23:将 calendarId 替换为步骤 14 中的 calendarId
string calendarId = @"[email protected] /cdn-cgi/l/email-protection";
第 24 步:运行应用程序
第 25 步:访问您的日历,您应该会在其中看到该事件
2019/01/13