![](/img/trans.png)
[英]c# Google Calendar API V3 is ignoring my other account (multiple account authorization)
[英]Google calendar api v3 authorization and authentication with C#
您好,我找不到Google日历api v3授权和身份验证的工作代码(C#)? 看起来所有帖子都太旧了,并且库进行了更改,有人可以使用代码吗? 我向Google开发人员注册了项目,并获得了使用凭据。 谢谢。
这是一种静态方法,用于获取用于执行所有请求的CalendarService
对象,您需要将从Google Developers Dashboard下载的JSON文件存储在项目中的某个位置,然后在下面的代码中引用它。
首次运行此程序时,您将被重定向到网络浏览器中的Google登录屏幕,并被提示在此处访问日历。 确认后,下面将创建一个凭据文件,该文件将存储在用户计算机上指定的位置(例如,应用程序的安装目录)。 从那时起,将使用存储的凭证文件,并且您无需再次登录。
public static CalendarService GetService()
{
var Scopes = new string[] { CalendarService.Scope.Calendar };
UserCredential credential;
string filePath = Path.Combine("*YOUR DOWNLOADED JSON FILE LOCATION HERE*");
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
string credPath = "*WHERE YOU WILL STORE YOUR CREDENTIALS FILE*";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "*YOUR APPLICATION NAME HERE*",
});
return service;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.