google calendar v3 api with OAuth 2.0 service account return empty result -
i got 0 results calendar eventsresource.listrequest serviceaccountcredential. , same code works usercredential. don't exception or error, events.items count 0 when try auth key.p12 file.
i set api client access https://www.googleapis.com/auth/calendar on clientid of service account in domain admin security setting.
any ideas? thanks
--------------------------------here code:
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 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 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"); // correct use "primary" 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(); // empty serviceaccount auth, getting results user credential auth } catch (exception ex) { console.writeline(ex.message); // didnot exception either way } }
value events (when use usercredential)
{google.apis.calendar.v3.data.events} accessrole: "owner" defaultreminders: count = 1 description: null etag: "\"1417813280083000\"" items: count = 2 kind: "calendar#events" nextpagetoken: null nextsynctoken: null summary: "myemail@mydomain.net" timezone: "america/los_angeles" updated: {12/5/2014 1:01:20 pm} updatedraw: "2014-12-05t21:01:20.083z"
value events (when use 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"
Comments
Post a Comment