demoonedrive.pp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. program demoonedrive;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils, CustApp, jsonparser, fphttpwebclient, fpoauth2ini,
  5. restbase, odatabase, msgraph, office365client;
  6. type
  7. { TOneDriveDemoApplication }
  8. TOneDriveDemoApplication = class(TCustomApplication)
  9. private
  10. FSession,
  11. FLogFile,
  12. FConfig : String;
  13. FGS : TService;
  14. FGraph : TGraphService;
  15. FDrive : TDrive;
  16. FMaxLevel : Integer;
  17. FClient : TOffice365Client;
  18. Procedure DriveItemToTree(ALevel: Integer; AItem: TDriveItem);
  19. procedure EnsureService;
  20. procedure GetItemChildren(ALevel: Integer; R: TDriveItem);
  21. procedure GetDriveItems;
  22. procedure GetShareableLink(D: TDriveItem);
  23. protected
  24. procedure DoRun; override;
  25. public
  26. constructor Create(TheOwner: TComponent); override;
  27. destructor Destroy; override;
  28. procedure WriteHelp(Msg : String); virtual;
  29. end;
  30. procedure TOneDriveDemoApplication.EnsureService;
  31. Var
  32. FIS : TFPOAuth2IniStore;
  33. begin
  34. // Auth client
  35. FClient:=TOffice365Client.Create(Self);
  36. FClient.WebClient:=TFPHTTPWebClient.Create(Self);
  37. FClient.WebClient.LogFile:=FLogFile;
  38. FClient.AuthHandler:=TAzureADOAuth2Handler.Create(Self);
  39. FIS:=TFPOAuth2IniStore.Create(Self);
  40. FIS.ConfigFileName:=FConfig;
  41. FIS.SessionFileName:=FConfig;
  42. FClient.AuthHandler.Store:=FIS;
  43. FClient.AuthHandler.LoadConfig();
  44. FClient.AuthHandler.LoadSession(FSession);
  45. FClient.AuthHandler.Config.AuthScope:='https://graph.microsoft.com/';
  46. FClient.WebClient.RequestSigner:=FClient.AuthHandler;
  47. FClient.AuthHandler.WebClient:=FClient.WebClient;
  48. // Service
  49. FGS:=TService.Create(Self);
  50. FGS.ServiceURL:='https://graph.microsoft.com/v1.0/';
  51. FGS.APINeedsAuth:=True;
  52. FGS.WebClient:=FClient.WebClient;
  53. // Default container
  54. FGraph:=FGS.GraphService;
  55. end;
  56. Procedure TOneDriveDemoApplication.DriveItemToTree(ALevel : Integer; AItem : TDriveItem) ;
  57. Var
  58. S : String;
  59. I : integer;
  60. begin
  61. S:='';
  62. For I:=1 to Alevel do
  63. S:=S+' ';
  64. if Assigned(AItem.folder) then
  65. S:=S+'*- '
  66. else
  67. S:=S+'+- ';
  68. Writeln(S,AItem.Name+' (URL: '+AItem.webUrl+')');
  69. end;
  70. procedure TOneDriveDemoApplication.GetItemChildren(ALevel: Integer;
  71. R: TDriveItem);
  72. Var
  73. AR : TDriveItemArray;
  74. I : Integer;
  75. begin
  76. if Assigned(R.folder) and (R.folder.childCount>0) then
  77. begin
  78. AR:=FDrive.items(FGS).Get(R.id).children(FGS).ListAll('');
  79. For I:=0 to Length(AR)-1 do
  80. begin
  81. DriveItemToTree(ALevel,AR[I]);
  82. if (ALevel<FMaxLevel) then
  83. GetItemChildren(ALevel+1,AR[I]);
  84. end;
  85. end;
  86. end;
  87. procedure TOneDriveDemoApplication.GetDriveItems;
  88. Var
  89. Me : TUser;
  90. R : TDriveItem;
  91. AR : TDriveItemArray;
  92. I,L : Integer;
  93. begin
  94. Me:=FGraph.me;
  95. FDrive:=Me.drive(FGS);
  96. R:=FDrive.root(FGS);
  97. DriveItemToTree(0,R);
  98. // AR:=TDriveItemArray(FGS.ArrayServiceCall(FGS.ServiceURL+'me/drive/root/children',TdriveItem).Data);
  99. AR:=R.children(FGS).ListAll('');
  100. L:=Length(AR);
  101. For I:=0 to L-1 do
  102. begin
  103. DriveItemToTree(1,AR[I]);
  104. if Assigned(AR[I].folder) and (AR[I].folder.childCount>0) then
  105. GetItemChildren(2,AR[I]);
  106. end;
  107. if L>0 then
  108. GetShareableLink(AR[L-1]);
  109. end;
  110. procedure TOneDriveDemoApplication.GetShareableLink(D : TDriveItem);
  111. Var
  112. P : Tpermission;
  113. begin
  114. Writeln('Getting shareable link for item(',D.id, ') : ',D.name);
  115. // By default, the Office365 API doesn't return @odata.id,
  116. // and drive items are contained (i.e. not directly accessible through a entityset)
  117. // so we set the base path manually.
  118. D.BasePath:='/me/drive/items/'+D.ID;
  119. P:=D.createLink(FGS,'view','organization');
  120. try
  121. if not Assigned(P.link) then
  122. Writeln('No permissions link created')
  123. else
  124. Writeln('Shareable URL: ',P.link.webUrl);
  125. finally
  126. P.Free;
  127. end;
  128. end;
  129. { TOneDriveDemoApplication }
  130. procedure TOneDriveDemoApplication.DoRun;
  131. var
  132. ErrorMsg: String;
  133. begin
  134. // quick check parameters
  135. ErrorMsg:=CheckOptions('hl:r:c:s:',['help','recurse:','logfile:','config:','session:']);
  136. if (ErrorMsg<>'') or HasOption('h', 'help') then
  137. WriteHelp(ErrorMsg);
  138. FLogFile:=GetOptionValue('l','logfile');
  139. if FLogFile='' then
  140. FLogFile:=ExtractFilePath(ParamStr(0))+'requests.log';
  141. FConfig:=GetOptionValue('c','config');
  142. if FConfig='' then
  143. FConfig:=ExtractFilePath(ParamStr(0))+'msgraph.ini';
  144. FMaxLevel:=StrToIntDef(GetOptionValue('r','recurse'),3);
  145. FSession:=GetOptionValue('s','session');
  146. if (FSession='') then
  147. WriteHelp('Need session');
  148. If FMaxLevel<2 then
  149. FMaxLevel:=2;
  150. EnsureService;
  151. GetDriveItems;
  152. Terminate;
  153. end;
  154. constructor TOneDriveDemoApplication.Create(TheOwner: TComponent);
  155. begin
  156. inherited Create(TheOwner);
  157. StopOnException:=True;
  158. end;
  159. destructor TOneDriveDemoApplication.Destroy;
  160. begin
  161. inherited Destroy;
  162. end;
  163. procedure TOneDriveDemoApplication.WriteHelp(Msg: String);
  164. begin
  165. If Msg<>'' then
  166. Writeln('Error : ',Msg);
  167. Writeln('Usage: ', ExeName, ' -s session [options]');
  168. Writeln('Where options : ');
  169. Writeln('-h --help this help');
  170. Writeln('-c --config config file with session and client data (default msgraph.ini)');
  171. Writeln('-l --logfile config file with session and client data (default requests.log)');
  172. Writeln('-s --session=name session to load from config file');
  173. Writeln('-r --recurse=level maximum recursion level (defult 2)');
  174. Halt(Ord(Msg<>''));
  175. end;
  176. var
  177. Application: TOneDriveDemoApplication;
  178. begin
  179. Application:=TOneDriveDemoApplication.Create(nil);
  180. Application.Title:='OneDrive Demo Command-line Application';
  181. Application.Run;
  182. Application.Free;
  183. end.