oauth 2.0 - How to access domain users' calendar using service account in .net? -
i using service account key.p12 cert access google calendar api. however, cannot access user's calendar in domain. did follow steps delegating domain-wide authority service account https://developers.google.com/accounts/docs/oauth2serviceaccount
it not have code sample .net. , seems serviceaccountcredential in google .net client library. here code
static void main(string[] args) { string serviceaccountemail = "xxxx@developer.gserviceaccount.com"; var certificate = new x509certificate2(@"clientprivatekey.p12", "notasecret", x509keystorageflags.exportable); console.writeline("service account: {0}", serviceaccountemail); serviceaccountcredential credential = new serviceaccountcredential(new serviceaccountcredential.initializer(serviceaccountemail) { scopes = new[] { calendarservice.scope.calendar } }.fromcertificate(certificate)); baseclientservice.initializer initializer = new baseclientservice.initializer(); initializer.httpclientinitializer = credential; initializer.applicationname = "google calendar sample"; calendarservice calservice = new calendarservice(initializer); // list calendars can see try { var list = calservice.calendarlist.list().execute(); if (list.items.count > 0) { foreach(var item in list.items) { console.writeline("found calendar account {0}", item.id); } } else { console.writeline("calendar list service account empty"); } } catch(exception ex) { console.writeline(ex.message); } }
the calendar list empty. if manually share domain account calendar service account in calendar setting, code returns domain account calendar successfully.
is there way make service account access user's calendar in domain?
actually, code should use service account "impersonate" domain users 1 one, rather trying share calendars service account.
serviceaccountcredential.initializer(serviceaccountemail) { scopes = new[] { calendarservice.scope.calendar }, user = "myaccount@mydomain.com" // impersonate domain user }.fromcertificate(certificate));
also need follow steps delegating domain-wide authority service account in google domain admin console, , add right scope( calendar, https://www.googleapis.com/auth/calendar )
Comments
Post a Comment