带有OAuth 2.0服务帐户的google calendar v3 api返回空结果
我从带有ServiceAccountCredential的Calendar EventsResource.ListRequest获得零结果。同样的代码也适用于UserCredential。我没有得到任何异常或错误,只有事件。当我尝试使用key.p12文件进行身份验证时,项目计数始终为零 我还在域管理安全设置中为此服务帐户的clientID设置API客户端访问权限 有什么想法吗?谢谢 ----------------这是我的密码:带有OAuth 2.0服务帐户的google calendar v3 api返回空结果,calendar,oauth-2.0,google-api,is-empty,service-accounts,Calendar,Oauth 2.0,Google Api,Is Empty,Service Accounts,我从带有ServiceAccountCredential的Calendar EventsResource.ListRequest获得零结果。同样的代码也适用于UserCredential。我没有得到任何异常或错误,只有事件。当我尝试使用key.p12文件进行身份验证时,项目计数始终为零 我还在域管理安全设置中为此服务帐户的clientID设置API客户端访问权限 有什么想法吗?谢谢 ----------------这是我的密码: static UserCredential getUser
static UserCredential getUserCredential()
{
UserCredential credential;
using (var stream = new FileStream(@"client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { CalendarService.Scope.CalendarReadonly },
"user", CancellationToken.None, new FileDataStore("carol test")).Result;
}
return credential;
}
static ServiceAccountCredential getServiceAccountCredential()
{
//Google authentication using OAuth for single calendar
string serviceAccountEmail = "xx...@developer.gserviceaccount.com";
var certificate = new X509Certificate2(@"clientPrivateKey.p12", "notasecret",
X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(new
ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes =
new[] { CalendarService.Scope.Calendar }
}.FromCertificate(certificate));
return credential;
}
static void Main(string[] args)
{
// Create the service.
BaseClientService.Initializer initializer = new
BaseClientService.Initializer();
//initializer.HttpClientInitializer = getUserCredential();
initializer.HttpClientInitializer = getServiceAccountCredential();
initializer.ApplicationName = "Google Calendar Sample";
CalendarService calservice = new CalendarService(initializer);
List<CalendarEvent> calendarEvents = new List<CalendarEvent>();
try
{
EventsResource.ListRequest req = calservice.Events.List("primary"); // Is it correct to use "primary" as calendarID?
req.TimeMin = new DateTime(2014, 12, 1);
req.TimeMax = new DateTime(2014, 12, 6);
req.SingleEvents = true;
req.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
Events events = req.Execute(); // always empty for ServiceAccount auth, but getting results for user credential auth
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); // didnot get exception either way
}
}
使用service account key.p12 auth时事件的值
{Google.Apis.Calendar.v3.Data.Events}
AccessRole: "owner"
DefaultReminders: Count = 0
Description: null
ETag: "\"1417747728066000\""
Items: Count = 0
Kind: "calendar#events"
NextPageToken: null
NextSyncToken: null
Summary: "xxxx@developer.gserviceaccount.com"
TimeZone: "UTC"
Updated: {12/4/2014 6:48:48 PM}
UpdatedRaw: "2014-12-05T02:48:48.066Z"
问题是我的gmail帐户日历没有与我的服务帐户共享。如果我与我的服务帐户手动共享日历,则一切正常。我将创建一个关于如何为服务帐户设置日历域访问权限的新问题。答案如下:
{Google.Apis.Calendar.v3.Data.Events}
AccessRole: "owner"
DefaultReminders: Count = 0
Description: null
ETag: "\"1417747728066000\""
Items: Count = 0
Kind: "calendar#events"
NextPageToken: null
NextSyncToken: null
Summary: "xxxx@developer.gserviceaccount.com"
TimeZone: "UTC"
Updated: {12/4/2014 6:48:48 PM}
UpdatedRaw: "2014-12-05T02:48:48.066Z"