diff --git a/kgOrdersServer/Source/qbAPI.pas b/kgOrdersServer/Source/qbAPI.pas index 242129c..951dc96 100644 --- a/kgOrdersServer/Source/qbAPI.pas +++ b/kgOrdersServer/Source/qbAPI.pas @@ -10,7 +10,7 @@ uses System.Generics.Collections, AdvEdit, vcl.wwdblook, vcl.wwdbdatetimepicker, System.Hash, Api.Database, Vcl.ExtCtrls, WEBLib.Forms, WEBLib.Controls, WEBLib.StdCtrls, WEBLib.ExtCtrls, WEBLib.REST, WEBLib.WebTools,System.Net.HttpClient, - System.Net.URLClient, System.Net.HttpClientComponent; + System.Net.URLClient, System.Net.HttpClientComponent, System.netencoding; type @@ -25,6 +25,7 @@ type { Public declarations } procedure getCompanyInfo(); procedure getAccessToken(); + //function refreshAccessToken(): boolean; end; var @@ -40,6 +41,8 @@ begin //getCompanyInfo(); end; + + procedure TfQB.getAccessToken(); var restClient: TRESTClient; @@ -63,54 +66,45 @@ begin client := 'ABgO14uvjh8XqLud7spQ8lkb98AUpcdA7HbyMJfCAtl65sQ5yy'; secret := 'bQ06TRemHeAGFzVHRaTUvUoBU9jpU9itK6MOMgqN'; companyID := '9341454272655710'; - RefreshToken := 'AB11751305638q5pAyQI2MPvhkIGjH1ur3PRsas2zSKGGcC4vs'; - EncodedAuth := 'QUJnTzE0dXZqaDhYcUx1ZDdzcFE4bGtiOThBVXBjZEE3SGJ5TUpmQ0F0bDY1c1E1eXk6YlEwNlRSZW1IZUFHRnpWSFJhVFV2VW9CVTlqcFU5aXRLNk1PTWdxTg=='; - - + RefreshToken := 'AB11751554594LX59YtRB69e6vWUjg56d9zCWjOOUisSTd6VfC'; + //EncodedAuth := 'QUJnTzE0dXZqaDhYcUx1ZDdzcFE4bGtiOThBVXBjZEE3SGJ5TUpmQ0F0bDY1c1E1eXk6YlEwNlRSZW1IZUFHRnpWSFJhVFV2VW9CVTlqcFU5aXRLNk1PTWdxTg=='; + EncodedAuth := TNetEncoding.Base64.Encode(client + ':' + secret); // Prepare the REST client and request - restClient := TRESTClient.Create('https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer'); + restClient := TRESTClient.Create(nil); restRequest := TRESTRequest.Create(nil); restRequest.Client := restClient; restResponse := TRESTResponse.Create(nil); restRequest.Response := restResponse; - restRequest.Method := rmPOST; + restClient.BaseURL := 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer'; - // Set the Accept header (application/json) - restRequest.Params.Clear; + restRequest.Method := rmPOST; // Add Content-Type and Accept headers - restRequest.AddParameter('Content-Type', 'application/x-www-form-urlencoded', pkHTTPHEADER); - restRequest.AddParameter('Accept', 'application/json', pkHTTPHEADER); - restRequest.AddParameter('Authorization', 'Basic ' + EncodedAuth, pkHTTPHEADER); + restClient.Accept := 'application/json'; + restClient.ContentType := 'application/x-www-form-urlencoded'; + //restClient.RaiseExceptionOn500 := false; - // Construct body content like -d "grant_type=refresh_token&refresh_token=AB11751305638q5pAyQI2MPvhkIGjH1ur3PRsas2zSKGGcC4vs" - body := 'grant_type=refresh_token&refresh_token=' + RefreshToken; - - // Add body parameters as form-urlencoded data - restRequest.AddBody(body, TRESTContentType.ctAPPLICATION_X_WWW_FORM_URLENCODED); + restRequest.Params.Clear; + Memo1.Lines.add(restRequest.ContentType); + param := restRequest.Params.AddItem; + param.Name := 'Authorization'; + param.Kind := pkHTTPHEADER; + param.Options := param.Options + [TRESTRequestParameterOption.poDoNotEncode]; + param.Value := 'Basic ' + EncodedAuth; + //restRequest.Params.AddHeader('Authorization', 'Basic ' + EncodedAuth); - // Log the request URL and method - Memo1.Lines.Add('Request URL: ' + restRequest.Client.BaseURL + restRequest.Resource); + restRequest.Params.AddItem('grant_type', 'refresh_token'); + restRequest.Params.AddItem('refresh_token', RefreshToken); - // Log headers - Memo1.Lines.Add('Headers:'); - for item in restRequest.Params do - begin - Memo1.Lines.Add(item.Value); - end; + for item in restRequest.Params do + Memo1.Lines.Add(item.Name + ':' + item.Value); + restRequest.Method := rmPOST; - // Log body (raw form data) - Memo1.Lines.Add('Body:'); - Memo1.Lines.Add(body); - Memo1.Lines.Add('--- Request Logged ---'); restRequest.Execute; - // Log response for debugging - Memo1.Lines.Add('Status Code: ' + IntToStr(restResponse.StatusCode)); - Memo1.Lines.Add('Response: ' + restResponse.Content); - Memo1.Lines.Add('Response Headers: ' + restResponse.Headers.Text); + //Memo1.Lines.Add(restRequest.Response.Content); restClient.Free; restRequest.Free; diff --git a/kgOrdersServer/kgOrdersServer.ini b/kgOrdersServer/kgOrdersServer.ini index cd66476..bfb5a79 100644 --- a/kgOrdersServer/kgOrdersServer.ini +++ b/kgOrdersServer/kgOrdersServer.ini @@ -1,7 +1,7 @@ [Settings] MemoLogLevel=4 FileLogLevel=5 -LogFileNum=354 +LogFileNum=372 webClientVersion=1.0.0 [Database]