{$mode objfpc}{$H+}
uses
googleservice,
googleclient,
googlestorage,
openssl,
jsonparser,
fpjson,
fpoauth2,
fpwebclient,
fphttpwebclient;
type
TAuthHelper = class
procedure DoUserConsent(const AURL: String; out AAuthCode: String);
end;
procedure TAuthHelper.DoUserConsent(const AURL: String; out AAuthCode: String);
begin
WriteLn('Open the following URL in browser:');
WriteLn(AURL);
Write('Writeback the resulting code here: ');
ReadLn(AAuthCode);
end;
var
AuthHelper: TAuthHelper;
GClient: TGoogleClient;
GStorageAPI: TStorageAPI;
BucketsResource: TBucketsResource;
BucketsListOptions: TBucketsListOptions;
Buckets: TBuckets;
i: Integer;
begin
try
// Register Tasks resources.
TStorageAPI.RegisterAPIResources;
// Set up google client.
GClient := TGoogleClient.Create(nil);
GClient.WebClient := TFPHTTPWebClient.Create(nil);
GClient.WebClient.RequestSigner := GClient.AuthHandler;
GClient.WebClient.LogFile := 'requests.log';
GClient.AuthHandler.WebClient := GClient.WebClient;
GClient.AuthHandler.Config.AccessType := atOffLine;
// We want to enter a code.
AuthHelper := TAuthHelper.Create;
GClient.OnUserConsent := @AuthHelper.DoUserConsent;
// Create a Tasks API and connect it to the client.
GStorageAPI := TStorageAPI.Create(nil);
GStorageAPI.GoogleClient := GClient;
// Registered application needs tasks scope
GClient.AuthHandler.Config.ClientID := '<my client id>';
GClient.AuthHandler.Config.ClientSecret := '<my client secret>';
GClient.AuthHandler.Config.AuthScope := 'https://www.googleapis.com/auth/devstorage.read_only';
// We are offline.
GClient.AuthHandler.Config.RedirectUri := 'urn:ietf:wg:oauth:2.0:oob';
// Session data
GClient.AuthHandler.Session.RefreshToken := '';
GClient.AuthHandler.Session.AccessToken := '';
GClient.AuthHandler.Session.AuthTokenType := '';
GClient.AuthHandler.Session.AuthExpires := 0;
GClient.AuthHandler.Session.AuthExpiryPeriod := 0;
BucketsResource := GStorageAPI.CreateBucketsResource;
BucketsListOptions.Project := '<my project number>';
Buckets := BucketsResource.List(BucketsListOptions);
if Assigned(Buckets) then
for i := 0 to Length(Buckets.Items) - 1 do
WriteLn(Buckets.Items[i].name + ': ' + Buckets.Items[i].selfLink)
else
WriteLn('Failed to list bucket');
finally
AuthHelper.Free;
GStorageAPI.Free;
GClient.Free;
end;
end.