.net core 在开发者帐户中创建Google日历事件
我需要在一个指定的google帐户中创建一个日历事件。我一直在访问Api文档,它只显示了如何使用OAuth2在用户的电子邮件帐户中创建事件。但是,如果我已经有权访问用户名和密码,如何使用.NETCore为指定的google帐户执行此操作 您不能使用登录名和密码登录到google api这称为客户端登录,google在2015年5月关闭了此功能 您可以做的是创建一个服务帐户。服务帐户是虚拟用户。如果您使用服务帐户电子邮件地址并与服务帐户共享您想要访问的此日历,它将能够创建事件.net core 在开发者帐户中创建Google日历事件,.net-core,google-oauth,google-calendar-api,.net Core,Google Oauth,Google Calendar Api,我需要在一个指定的google帐户中创建一个日历事件。我一直在访问Api文档,它只显示了如何使用OAuth2在用户的电子邮件帐户中创建事件。但是,如果我已经有权访问用户名和密码,如何使用.NETCore为指定的google帐户执行此操作 您不能使用登录名和密码登录到google api这称为客户端登录,google在2015年5月关闭了此功能 您可以做的是创建一个服务帐户。服务帐户是虚拟用户。如果您使用服务帐户电子邮件地址并与服务帐户共享您想要访问的此日历,它将能够创建事件 using Goog
using Google.Apis.Calendar.v3;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
namespace GoogleSamplecSharpSample.Calendarv3.Auth
{
public static class ServiceAccountExample
{
/// <summary>
/// Authenticating to Google using a Service account
/// Documentation: https://developers.google.com/accounts/docs/OAuth2#serviceaccount
/// </summary>
/// <param name="serviceAccountEmail">From Google Developer console https://console.developers.google.com</param>
/// <param name="serviceAccountCredentialFilePath">Location of the .p12 or Json Service account key file downloaded from Google Developer console https://console.developers.google.com</param>
/// <returns>AnalyticsService used to make requests against the Analytics API</returns>
public static CalendarService AuthenticateServiceAccount(string serviceAccountEmail, string serviceAccountCredentialFilePath, string[] scopes)
{
try
{
if (string.IsNullOrEmpty(serviceAccountCredentialFilePath))
throw new Exception("Path to the service account credentials file is required.");
if (!File.Exists(serviceAccountCredentialFilePath))
throw new Exception("The service account credentials file does not exist at: " + serviceAccountCredentialFilePath);
if (string.IsNullOrEmpty(serviceAccountEmail))
throw new Exception("ServiceAccountEmail is required.");
// For Json file
if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".json")
{
GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes);
}
// Create the Analytics service.
return new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Calendar Service account Authentication Sample",
});
}
else if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".p12")
{ // If its a P12 file
var certificate = new X509Certificate2(serviceAccountCredentialFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
// Create the Calendar service.
return new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Calendar Authentication Sample",
});
}
else
{
throw new Exception("Unsupported Service accounts credentials.");
}
}
catch (Exception ex)
{
throw new Exception("CreateServiceAccountCalendarFailed", ex);
}
}
}
}
使用Google.api.Calendar.v3;
使用Google.api.Auth.OAuth2;
使用Google.api.Services;
使用制度;
使用System.IO;
使用System.Security.Cryptography.X509证书;
名称空间GoogleSamplecSharpSample.Calendarv3.Auth
{
公共静态类ServiceAccountExample
{
///
///使用服务帐户向Google进行身份验证
///文件:https://developers.google.com/accounts/docs/OAuth2#serviceaccount
///
///从谷歌开发者控制台https://console.developers.google.com
///从Google开发者控制台下载的.p12或Json服务帐户密钥文件的位置https://console.developers.google.com
///AnalyticsService用于针对Analytics API发出请求
公共静态日历服务验证ServiceAccount(字符串serviceAccountEmail、字符串serviceAccountCredentialFilePath、字符串[]作用域)
{
尝试
{
if(string.IsNullOrEmpty(serviceAccountCredentialFilePath))
抛出新异常(“需要服务帐户凭据文件的路径”);
如果(!File.Exists(serviceAccountCredentialFilePath))
抛出新异常(“服务帐户凭据文件不存在于:“+serviceAccountCredentialFilePath”);
if(string.IsNullOrEmpty(serviceAccountEmail))
抛出新异常(“需要ServiceAccountEmail”);
//对于Json文件
if(Path.GetExtension(serviceAccountCredentialFilePath.ToLower()==“.json”)
{
谷歌认证证书;
使用(var stream=newfilestream(serviceAccountCredentialFilePath,FileMode.Open,FileAccess.Read))
{
credential=GoogleCredential.FromStream(流)
.CreateScoped(范围);
}
//创建分析服务。
返回新的CalendarService(新的BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=“日历服务帐户验证示例”,
});
}
else if(Path.GetExtension(serviceAccountCredentialFilePath.ToLower()==“.p12”)
{//如果是P12文件
var证书=新的X509Certificate2(serviceAccountCredentialFilePath,“notasecret”,X509KeyStrageFlags.MachineKeySet | X509KeyStrageFlags.Exportable);
var凭证=新ServiceAccountCredential(新ServiceAccountCredential.Initializer(serviceAccountEmail)
{
范围=范围
}.FromCertificate(证书));
//创建日历服务。
返回新的CalendarService(新的BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=“日历验证示例”,
});
}
其他的
{
抛出新异常(“不支持的服务帐户凭据”);
}
}
捕获(例外情况除外)
{
抛出新异常(“CreateServiceAccountCalendarFailed”,ex);
}
}
}
}
从我的日历示例项目中提取的代码