calendardemo.lpr 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. program calendardemo;
  2. {$mode objfpc}{$H+}
  3. uses
  4. {$IFDEF UNIX}{$IFDEF UseCThreads}
  5. cthreads,
  6. {$ENDIF}{$ENDIF}
  7. Classes, SysUtils, CustApp, fpoauth2ini, fphttpwebclient, fpoauth2, jsonparser,
  8. IniFiles, googlebase, googleservice, googleclient, googlecalendar, opensslsockets;
  9. type
  10. { TGoogleCalendarApplication }
  11. TGoogleCalendarApplication = class(TCustomApplication)
  12. private
  13. FSession,
  14. FLogFile,
  15. FConfig : String;
  16. FClient : TGoogleClient;
  17. FCalendarAPI: TCalendarAPI;
  18. procedure DoUserConsent(const AURL: String; out AAuthCode: String);
  19. procedure EnsureService;
  20. procedure ListCalendars;
  21. procedure ListEvents(aCalendarID: String);
  22. protected
  23. procedure DoRun; override;
  24. public
  25. constructor Create(TheOwner: TComponent); override;
  26. destructor Destroy; override;
  27. procedure WriteHelp(const Msg: String); virtual;
  28. end;
  29. procedure TGoogleCalendarApplication.ListCalendars;
  30. var
  31. Entry: TCalendarListEntry;
  32. Resource : TCalendarListResource;
  33. EN : String;
  34. i:integer;
  35. CalendarList: TCalendarList;
  36. begin
  37. Resource:=Nil;
  38. try
  39. Resource:=FCalendarAPI.CreateCalendarListResource;
  40. CalendarList:=Resource.list('');
  41. I:=0;
  42. if assigned(CalendarList) then
  43. for Entry in CalendarList.items do
  44. begin
  45. Inc(i);
  46. EN:=Entry.ID;
  47. if Entry.Summary='' then
  48. EN:=EN+' ('+Entry.description+')'
  49. else
  50. EN:=EN+' ('+Entry.Summary+')';
  51. Writeln(I,': ID: ',EN);
  52. end;
  53. finally
  54. FClient.AuthHandler.SaveSession('me');
  55. FreeAndNil(Resource);
  56. FreeAndNil(CalendarList);
  57. end;
  58. end;
  59. procedure TGoogleCalendarApplication.ListEvents(aCalendarID : String);
  60. var
  61. Events : TEvents;
  62. Entry: TEvent;
  63. EN : String;
  64. i:integer;
  65. begin
  66. Events:=FCalendarAPI.EventsResource.list(aCalendarid,'');
  67. try
  68. I:=0;
  69. if assigned(Events) then
  70. for Entry in Events.items do
  71. begin
  72. Inc(i);
  73. EN:=Entry.Summary;
  74. if EN='' then
  75. EN:=Entry.id+' ('+Entry.description+')';
  76. if Assigned(Entry.Start) then
  77. if Entry.start.date<>0 then
  78. EN:=DateToStr(Entry.start.date)+' : '+EN
  79. else if Entry.start.dateTime<>0 then
  80. EN:=DateTimeToStr(Entry.start.datetime)+' : '+EN
  81. else
  82. EN:='(unspecified time) '+EN;
  83. Writeln(i,': '+EN);
  84. end;
  85. Finally
  86. FClient.AuthHandler.SaveSession(FSession);
  87. Events.Free;
  88. end;
  89. end;
  90. procedure TGoogleCalendarApplication.EnsureService;
  91. { TGoogleCalendarApplication }
  92. Var
  93. FIS : TFPOAuth2IniStore;
  94. begin
  95. // Auth client
  96. Writeln('Creating client');
  97. FCLient:=TGoogleClient.Create(Self);
  98. FClient.WebClient:=TFPHTTPWebClient.Create(Self);
  99. FClient.WebClient.LogFile:=FLogFile;
  100. FClient.AuthHandler:=TGoogleOAuth2Handler.Create(Self);
  101. Writeln('Creating client config store');
  102. FIS:=TFPOAuth2IniStore.Create(Self);
  103. FIS.ConfigFileName:=FConfig;
  104. FIS.SessionFileName:=FConfig;
  105. FClient.AuthHandler.Store:=FIS;
  106. Writeln('Loading config');
  107. FClient.AuthHandler.LoadConfig();
  108. Writeln('Loading session');
  109. FClient.AuthHandler.LoadSession(FSession);
  110. Writeln('Configuring local client');
  111. FClient.AuthHandler.Config.AuthScope:='https://www.googleapis.com/auth/calendar';
  112. FClient.AuthHandler.Config.AccessType:=atOffline;
  113. FClient.AuthHandler.Config.RedirectUri:='urn:ietf:wg:oauth:2.0:oob';
  114. // We want to enter a code.
  115. Fclient.OnUserConsent := @DoUserConsent;
  116. FClient.WebClient.RequestSigner:=FClient.AuthHandler;
  117. FClient.AuthHandler.WebClient:=FClient.WebClient;
  118. Writeln('Creating API');
  119. FCalendarAPI:=TCalendarAPI.Create(Self);
  120. FCalendarAPI.GoogleClient:=FClient;
  121. end;
  122. procedure TGoogleCalendarApplication.DoUserConsent(Const AURL: String; Out AAuthCode: String);
  123. begin
  124. Writeln('');
  125. writeln('User consent required. Please open following URL:' );
  126. Writeln('');
  127. writeln(AURL);
  128. Writeln('');
  129. writeln('And copy/paste the authorization code here:');
  130. Writeln('');
  131. write('Code: ');
  132. ReadLn(AAuthCode);
  133. Writeln('');
  134. writeln('End user consent, returning to Google API...');
  135. Writeln('');
  136. end;
  137. procedure TGoogleCalendarApplication.DoRun;
  138. var
  139. ErrorMsg: String;
  140. begin
  141. // quick check parameters
  142. ErrorMsg:=CheckOptions('ho::c:s:le:',['help','logfile::','config:','session:','list','entry:']);
  143. if (ErrorMsg<>'') or HasOption('h', 'help') then
  144. WriteHelp(ErrorMsg);
  145. FLogFile:=GetOptionValue('o','logfile');
  146. if FLogFile='' then
  147. FLogFile:=ExtractFilePath(ParamStr(0))+'requests.log';
  148. FConfig:=GetOptionValue('c','config');
  149. if FConfig='' then
  150. FConfig:=ExtractFilePath(ParamStr(0))+'google.ini';
  151. FSession:=GetOptionValue('s','session');
  152. if FSession='' then
  153. FSession:='me';
  154. EnsureService;
  155. if HasOption('l','list') then
  156. ListCalendars
  157. else if HasOption('e','entry') then
  158. ListEvents(GetOptionValue('e','entry'))
  159. else // Default
  160. ListCalendars;
  161. Terminate;
  162. end;
  163. constructor TGoogleCalendarApplication.Create(TheOwner: TComponent);
  164. begin
  165. inherited Create(TheOwner);
  166. StopOnException:=True;
  167. TCalendarAPI.RegisterAPIResources;
  168. end;
  169. destructor TGoogleCalendarApplication.Destroy;
  170. begin
  171. inherited Destroy;
  172. end;
  173. procedure TGoogleCalendarApplication.WriteHelp(const Msg: String);
  174. begin
  175. If Msg<>'' then
  176. Writeln('Error : ',Msg);
  177. Writeln('Usage: ', ExeName, ' -s session [options]');
  178. Writeln('Where options : ');
  179. Writeln('-h --help this help');
  180. Writeln('-c --config config file with session and client data (default msgraph.ini)');
  181. Writeln('-o --logfile config file with session and client data (default requests.log)');
  182. Writeln('-s --session=name session to load from config file');
  183. Writeln('-l --list list calendars (default action).');
  184. Writeln('-e --events=calID list events from calendar name "calID" (ID of calendar)');
  185. Halt(Ord(Msg<>''));
  186. end;
  187. var
  188. Application: TGoogleCalendarApplication;
  189. begin
  190. Application:=TGoogleCalendarApplication.Create(nil);
  191. Application.Title:='Google Calendar demo';
  192. Application.Run;
  193. Application.Free;
  194. end.