Commit 067a586d by Cam Hayes

Access and Refresh Token now save to file and beginning working on only getting…

Access and Refresh Token now save to file and beginning working on only getting a new access token every hour
parent 6418ca02
...@@ -25,11 +25,9 @@ type ...@@ -25,11 +25,9 @@ type
public public
{ Public declarations } { Public declarations }
procedure getCompanyInfo(); procedure getCompanyInfo();
procedure getAccessToken(); function RefreshAccessToken(): string;
procedure getToken();
//procedure forceModernTLS();
procedure ConfigureSSL(IOHandler: TIdSSLIOHandlerSocketOpenSSL); procedure ConfigureSSL(IOHandler: TIdSSLIOHandlerSocketOpenSSL);
//function refreshAccessToken(): boolean; procedure SaveTokens(AccessToken, RefreshToken: string);
end; end;
var var
...@@ -43,9 +41,25 @@ procedure TfQB.Button1Click(Sender: TObject); ...@@ -43,9 +41,25 @@ procedure TfQB.Button1Click(Sender: TObject);
begin begin
//getAccessToken(); //getAccessToken();
//getCompanyInfo(); //getCompanyInfo();
getToken() refreshAccessToken();
end; end;
procedure TfQB.SaveTokens(AccessToken, RefreshToken: string);
var
f: TStringList;
line: string;
begin
f := TStringList.Create;
f.Add('QUJnTzE0dXZqaDhYcUx1ZDdzcFE4bGtiOThBVXBjZEE3SGJ5TUpmQ0F0bDY1c1E1eXk6YlEwNlRSZW1IZUFHRnpWSFJhVFV2VW9CVTlqcFU5aXRLNk1PTWdxTg==#' +
AccessToken + '#' +
RefreshToken);
// Save to file (overwrites existing file)
f.SaveToFile('QB.txt');
f.Free;
end;
procedure TfQB.ConfigureSSL(IOHandler: TIdSSLIOHandlerSocketOpenSSL); procedure TfQB.ConfigureSSL(IOHandler: TIdSSLIOHandlerSocketOpenSSL);
begin begin
// For Indy 10.6.2+ (Delphi 10.2 Tokyo+) // For Indy 10.6.2+ (Delphi 10.2 Tokyo+)
...@@ -64,18 +78,20 @@ begin ...@@ -64,18 +78,20 @@ begin
IOHandler.SSLOptions.Mode := sslmClient; IOHandler.SSLOptions.Mode := sslmClient;
end; end;
procedure TfQB.GetToken; function TfQB.RefreshAccessToken: string;
// Refresh Token changes so make sure to save refresh token.
var var
IdHTTP: TIdHTTP; IdHTTP: TIdHTTP;
SSLIO: TIdSSLIOHandlerSocketOpenSSL; SSLIO: TIdSSLIOHandlerSocketOpenSSL;
RequestStream: TStringStream; RequestStream: TStringStream;
EncodedAuth, PostData: string; EncodedAuth, PostData, response, RefreshToken, AccessToken: string;
JSObj: TJSONObject;
begin begin
// 1. Encode credentials (same as working Postman request) // 1. Encode credentials (same as working Postman request)
EncodedAuth := 'QUJnTzE0dXZqaDhYcUx1ZDdzcFE4bGtiOThBVXBjZEE3SGJ5TUpmQ0F0bDY1c1E1eXk6YlEwNlRSZW1IZUFHRnpWSFJhVFV2VW9CVTlqcFU5aXRLNk1PTWdxTg=='; EncodedAuth := 'QUJnTzE0dXZqaDhYcUx1ZDdzcFE4bGtiOThBVXBjZEE3SGJ5TUpmQ0F0bDY1c1E1eXk6YlEwNlRSZW1IZUFHRnpWSFJhVFV2VW9CVTlqcFU5aXRLNk1PTWdxTg==';
// 2. Prepare POST data (EXACTLY as in Postman) // 2. Prepare POST data (EXACTLY as in Postman)
PostData := 'grant_type=refresh_token&refresh_token=AB11751554594LX59YtRB69e6vWUjg56d9zCWjOOUisSTd6VfC'; PostData := 'grant_type=refresh_token&refresh_token=AB11751646575Ns7nA3AniegiOuRWAak6CvqMigWqSIdwhAOEe';
// 3. Configure HTTP client // 3. Configure HTTP client
IdHTTP := TIdHTTP.Create(nil); IdHTTP := TIdHTTP.Create(nil);
...@@ -102,7 +118,14 @@ begin ...@@ -102,7 +118,14 @@ begin
// Execute POST // Execute POST
try try
Memo1.Lines.Add(IdHTTP.Post('https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer', RequestStream)); response := IdHTTP.Post('https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer', RequestStream);
Memo1.Lines.Add(response);
JSObj := TJSONObject.ParseJSONValue(response) as TJSONObject;
RefreshToken := JSObj.GetValue('refresh_token').ToString;
AccessToken := JSObj.GetValue('access_token').ToString;
SaveTokens(AccessToken, RefreshToken);
Memo1.Lines.Add(RefreshToken);
Memo1.Lines.Add(AccessToken);
except except
on E: EIdHTTPProtocolException do on E: EIdHTTPProtocolException do
Memo1.Lines.Add('Error: ' + E.Message + #13#10 + 'Response: ' + E.ErrorMessage); Memo1.Lines.Add('Error: ' + E.Message + #13#10 + 'Response: ' + E.ErrorMessage);
...@@ -116,75 +139,6 @@ begin ...@@ -116,75 +139,6 @@ begin
end; end;
end; end;
procedure TfQB.getAccessToken();
var
restClient: TRESTClient;
restRequest: TRESTRequest;
restResponse: TRESTResponse;
param: TRESTRequestParameter;
res: string;
sid, EncodedAuth, RefreshToken: string;
jsValue: TJSONValue;
jsObj, companyInfo: TJSONObject;
jaCalls: TJSONArray;
joCall: TJSONObject;
row: integer;
i: integer;
uri, temp, body: string;
sql: string;
companyID, client, secret: string;
pair: TJSONPair;
item: TRestRequestParameter;
postBodyStr: string;
begin
client := 'ABgO14uvjh8XqLud7spQ8lkb98AUpcdA7HbyMJfCAtl65sQ5yy';
secret := 'bQ06TRemHeAGFzVHRaTUvUoBU9jpU9itK6MOMgqN';
companyID := '9341454272655710';
RefreshToken := 'AB11751554594LX59YtRB69e6vWUjg56d9zCWjOOUisSTd6VfC';
//EncodedAuth := 'QUJnTzE0dXZqaDhYcUx1ZDdzcFE4bGtiOThBVXBjZEE3SGJ5TUpmQ0F0bDY1c1E1eXk6YlEwNlRSZW1IZUFHRnpWSFJhVFV2VW9CVTlqcFU5aXRLNk1PTWdxTg==';
EncodedAuth := TNetEncoding.Base64.Encode(client + ':' + secret);
// Prepare the REST client and request
restClient := TRESTClient.Create(nil);
restRequest := TRESTRequest.Create(nil);
restResponse := TRESTResponse.Create(nil);
restRequest.Client := restClient;
restRequest.Response := restResponse;
restClient.BaseURL := 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer';
// Set request method to POST
restRequest.Method := rmPOST;
// Set Content-Type and Accept headers
restRequest.Params.Clear;
postBodyStr := 'grant_type=refresh_token&' + #13#10 +
'refresh_token=' + RefreshToken;
restRequest.Params.AddHeader('Authorization', 'Basic QUJnTzE0dXZqaDhYcUx1ZDdzcFE4bGtiOThBVXBjZEE3SGJ5TUpmQ0F0bDY1c1E1eXk6YlEwNlRSZW1IZUFHRnpWSFJhVFV2VW9CVTlqcFU5aXRLNk1PTWdxTg==');
restRequest.Params.AddHeader('Content-Type', 'application/x-www-form-urlencoded');
restRequest.params.AddHeader('Accept', 'application/json');
// Set the body with the properly formatted form data
restRequest.AddBody(postBodyStr, TRESTContentType.ctAPPLICATION_X_WWW_FORM_URLENCODED);
// Execute the request
try
restRequest.Execute;
// Optionally log the response to the memo for debugging
Memo1.Lines.Add('Response: ' + restResponse.Content);
except
on E: Exception do
Memo1.Lines.Add('Error: ' + E.Message);
end;
// Free resources
restClient.Free;
restRequest.Free;
restResponse.Free;
end;
procedure TfQB.getCompanyInfo(); procedure TfQB.getCompanyInfo();
var var
restClient: TRESTClient; restClient: TRESTClient;
......
[Settings] [Settings]
MemoLogLevel=4 MemoLogLevel=4
FileLogLevel=5 FileLogLevel=5
LogFileNum=385 LogFileNum=397
webClientVersion=1.0.0 webClientVersion=1.0.0
[Database] [Database]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment