c# - Multiple Google access permissions for a desktop application -


i'm writing desktop application in c# should able access users on google apps "account" retrieve calendar-events each user. have added calendar api , admin sdk "project".

both methods (below) works fine on own when want authorize app both apis following permission errors.

insufficient permission [403]

invalid_grant", description:"token has been revoked.

this made me wonder if possible ask permissions when application starts, instead of authorizing "features" separately?

    static string[] calendarscopes = {calendarservice.scope.calendarreadonly };     static string[] directoryscopes = {    directoryservice.scope.admindirectoryuserreadonly };      private static void googlecalendar()     {         usercredential credential;          using (var stream = new filestream("client_secret.json", filemode.open, fileaccess.read))         {             string credpath = environment.getfolderpath(environment.specialfolder.personal);             credpath = path.combine(credpath, ".credentials");              credential = googlewebauthorizationbroker.authorizeasync(                 googleclientsecrets.load(stream).secrets,                 calendarscopes,                 "user",                 cancellationtoken.none,                 new filedatastore(credpath, true)).result;         }          // create google calendar api service.         var service = new calendarservice(new baseclientservice.initializer()         {             httpclientinitializer = credential,             applicationname = applicationname,         });          // define parameters of request.         eventsresource.listrequest request = service.events.list("primary");         request.timemin = datetime.now;         request.showdeleted = false;         request.singleevents = true;         //request.maxresults = 10;         request.orderby = eventsresource.listrequest.orderbyenum.starttime;          // list events.         events events = request.execute();         console.writeline("upcoming events:");         if (events.items != null && events.items.count > 0)         {             foreach (var eventitem in events.items)             {                 string when = eventitem.start.datetime.tostring();                 if (string.isnullorempty(when))                 {                     when = eventitem.start.date;                 }                 console.writeline("{0} ({1})", eventitem.summary, when);             }         }         else         {             console.writeline("no upcoming events found.");         }         console.read();     }      private static void googledirectory()     {         usercredential credential;          using (var stream =             new filestream("client_secret.json", filemode.open, fileaccess.read))         {             string credpath = environment.getfolderpath(                 environment.specialfolder.personal);             credpath = path.combine(credpath, ".credentials");              credential = googlewebauthorizationbroker.authorizeasync(                 googleclientsecrets.load(stream).secrets,                 directoryscopes,                 "user",                 cancellationtoken.none,                 new filedatastore(credpath, true)).result;             console.writeline("credential file saved to: " + credpath);         }          // create directory api service.         var service = new directoryservice(new baseclientservice.initializer()         {             httpclientinitializer = credential,             applicationname = applicationname,         });          // define parameters of request.         usersresource.listrequest request = service.users.list();         request.customer = "my_customer";         request.maxresults = 10;         request.orderby = usersresource.listrequest.orderbyenum.email;          // list users.          ilist<user> users = null;          try         {             users = request.execute().usersvalue;         }         catch (exception ex)         {             throw;         }           console.writeline("users:");         if (users != null && users.count > 0)         {             foreach (var useritem in users)             {                 console.writeline("{0} ({1})", useritem.primaryemail,                     useritem.name.fullname);             }         }         else         {             console.writeline("no users found.");         }         console.read();     } 

eureka! figured out, how solve problem, , quite simple.

the solution was, "merge" methods. meant, have check credentials once, using access token. added directory scope-array.

    static string[] scopes = { calendarservice.scope.calendarreadonly, directoryservice.scope.admindirectoryuserreadonly };      private static void googlethis()     {         usercredential credential;          using (var stream = new filestream("client_secret.json", filemode.open, fileaccess.read))         {             string credpath = environment.getfolderpath(environment.specialfolder.personal);             credpath = path.combine(credpath, ".credentials");              credential = googlewebauthorizationbroker.authorizeasync(                 googleclientsecrets.load(stream).secrets,                 scopes,                 "user",                 cancellationtoken.none,                 new filedatastore(credpath, true)).result;         }          // create google calendar api service.         var eventservice = new calendarservice(new baseclientservice.initializer()         {             httpclientinitializer = credential,             applicationname = applicationname,         });          // define parameters of request.         eventsresource.listrequest eventrequest = eventservice.events.list("primary");         eventrequest.timemin = datetime.now;         eventrequest.showdeleted = false;         eventrequest.singleevents = true;         //request.maxresults = 10;         eventrequest.orderby = eventsresource.listrequest.orderbyenum.starttime;          // list events.         events events = eventrequest.execute();         console.writeline("upcoming events:");         if (events.items != null && events.items.count > 0)         {             foreach (var eventitem in events.items)             {                 string when = eventitem.start.datetime.tostring();                 if (string.isnullorempty(when))                 {                     when = eventitem.start.date;                 }                 console.writeline("{0} ({1})", eventitem.summary, when);             }         }         else         {             console.writeline("no upcoming events found.");         }         // create directory api service.         var dirservice = new directoryservice(new baseclientservice.initializer()         {             httpclientinitializer = credential,             applicationname = applicationname,         });          // define parameters of request.         usersresource.listrequest dirrequest = dirservice.users.list();         dirrequest.customer = "my_customer";         dirrequest.maxresults = 10;         dirrequest.orderby = usersresource.listrequest.orderbyenum.email;          // list users.         ilist<user> users = null;         try         {             users = dirrequest.execute().usersvalue;             console.writeline("users:");             if (users != null && users.count > 0)             {                 foreach (var useritem in users)                 {                     console.writeline("{0} ({1})", useritem.primaryemail,                         useritem.name.fullname);                 }             }             else             {                 console.writeline("no users found.");             }         }         catch (exception ex)         {             throw;         }     } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -