!问题解决方案:我正在使用c# / .NET构建一个窗口表单,并且我想添加一个功能来在我的个人Google日历中添加一个事件。我已经在互联网上研究了几天了,但我发现的每一个解决方案似乎都被夸大了。
我只需要:
event title = textbox1.text;
event description = textbox2.text;
event date = datetimepicker.text;
addEvent();
但我不知道如何编写它,也找不到任何简单的解决方案(大多数解决方案都过于复杂)。我可以说,我有一些业余爱好编码经验,足以建立一个简单的项目,但我承认,这是超出我的水平,我需要一些指导。
我想我已经设置了Calendar API和Google。还在Visual中安装了Google包,但是如果有方法通过密码和用户名来连接,而不是API & SDK,我会很感激。
提前感谢!
!解决方案!
在@Dalmto的最初帮助下,我设法将他的代码链接到一个按钮上,并使它实际上触发了一个事件,因为以前它没有。
下面是我的最后一段代码,您可以简单地将其用作复制粘贴。把你的证件加进去。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System.IO;
using System.Threading;
using Google.Apis.Calendar.v3.Data;
namespace googleCalendarTesting
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string clientSecretJson = ""; //add your json path here
string userName = ""; // add your google account here
string[] scopes = new string[n] {"n1", "n2", "n3" }; // replace n with the number of scopes you need and write them one by one
CalendarService service = GetCalendarService(clientSecretJson, userName, scopes);
Event newEvent = new Event()
{
Summary = "event title",
Description = "event description",
Start = new EventDateTime()
{
DateTime = DateTime.Parse("2022-02-28T09:00:00-07:00"),
TimeZone = "America/Los_Angeles",
},
End = new EventDateTime()
{
DateTime = DateTime.Parse("2022-02-28T09:00:00-08:00"),
TimeZone = "America/Los_Angeles",
},
}; //// more options here https://developers.google.com/calendar/api/v3/reference/events/insert#.net
String calendarId = "primary"; // choose a calendar in your google account - you might have multiple calendars
EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
Event createdEvent = request.Execute();
}
public static CalendarService GetCalendarService(string clientSecretJson, string userName, string[] scopes)
{
try
{
if (string.IsNullOrEmpty(userName))
throw new ArgumentNullException("userName");
if (string.IsNullOrEmpty(clientSecretJson))
throw new ArgumentNullException("clientSecretJson");
if (!File.Exists(clientSecretJson))
throw new Exception("clientSecretJson file does not exist.");
var cred = GetUserCredential(clientSecretJson, userName, scopes);
return GetService(cred);
}
catch (Exception ex)
{
throw new Exception("Get Calendar service failed.", ex);
}
}
private static UserCredential GetUserCredential(string clientSecretJson, string userName, string[] scopes)
{
try
{
if (string.IsNullOrEmpty(userName))
throw new ArgumentNullException("userName");
if (string.IsNullOrEmpty(clientSecretJson))
throw new ArgumentNullException("clientSecretJson");
if (!File.Exists(clientSecretJson))
throw new Exception("clientSecretJson file does not exist.");
// These are the scopes of permissions you need. It is best to request only what you need and not all of them
using (var stream = new FileStream(clientSecretJson, FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
// Requesting Authentication or loading previously stored authentication for userName
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
scopes,
userName,
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
credential.GetAccessTokenForRequestAsync();
return credential;
}
}
catch (Exception ex)
{
throw new Exception("Get user credentials failed.", ex);
}
}
private static CalendarService GetService(UserCredential credential)
{
try
{
if (credential == null)
throw new ArgumentNullException("credential");
// Create Calendar API service.
return new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Calendar Oauth2 Authentication Sample"
});
}
catch (Exception ex)
{
throw new Exception("Get Calendar service failed.", ex);
}
}
}
}
我在项目中使用的范围:
https://www.googleapis.com/auth/calendar,
https://www.googleapis.com/auth/calendar.events,
https://www.googleapis.com/auth/calendar.events.readonly
发布于 2022-02-25 15:13:19
您需要一个来自(https://console.developers.google.com/apis/dashboard?pli=1)的API密钥,以便连接到日历,并将事件读写到日历上。普通用户名和密码将无法工作(据我所知)。Google的文档非常直接,并解释了每个步骤,以便从API中检索事件。https://developers.google.com/calendar/api/quickstart/dotnet
在了解了SDK之后,您可以增强应用程序将事件添加到日历中。如果我有时间,我将链接我的GitHub Repos中的一个例子,您可以参考。
编辑:正如DalmTo提到的,我没有创建API密钥。我在开发人员仪表板上创建了客户端凭据。快速启动指南中也提到了如何创建这些凭据。
https://stackoverflow.com/questions/71267723
复制相似问题