unit qbAPI;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, AdvUtil, Data.DB, Vcl.Grids, AdvObj,
  BaseGrid, AdvGrid, DBAdvGrid, MemDS, DBAccess, Uni, Vcl.StdCtrls, Vcl.Mask,
  vcl.wwdbedit, vcl.wwdotdot, vcl.wwdbcomb, REST.Client, REST.Types, System.JSON,
  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;


type
  TfQB = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
     httpReqTokenRefresh: TWebHttpRequest;
  public
    { Public declarations }
    procedure getCompanyInfo();
    procedure getAccessToken();
  end;

var
  fQB: TfQB;

implementation

{$R *.dfm}

procedure TfQB.Button1Click(Sender: TObject);
begin
  getAccessToken();
  //getCompanyInfo();
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;
begin
  client := 'ABgO14uvjh8XqLud7spQ8lkb98AUpcdA7HbyMJfCAtl65sQ5yy';
  secret := 'bQ06TRemHeAGFzVHRaTUvUoBU9jpU9itK6MOMgqN';
  companyID := '9341454272655710';
  RefreshToken := 'AB11751305638q5pAyQI2MPvhkIGjH1ur3PRsas2zSKGGcC4vs';
  EncodedAuth := 'QUJnTzE0dXZqaDhYcUx1ZDdzcFE4bGtiOThBVXBjZEE3SGJ5TUpmQ0F0bDY1c1E1eXk6YlEwNlRSZW1IZUFHRnpWSFJhVFV2VW9CVTlqcFU5aXRLNk1PTWdxTg==';


  // Prepare the REST client and request
  restClient := TRESTClient.Create('https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer');
  restRequest := TRESTRequest.Create(nil);
  restRequest.Client := restClient;
  restResponse := TRESTResponse.Create(nil);
  restRequest.Response := restResponse;

  restRequest.Method := rmPOST;

  // Set the Accept header (application/json)
    restRequest.Params.Clear;

  // 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);

  // 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);

    // Log the request URL and method
    Memo1.Lines.Add('Request URL: ' + restRequest.Client.BaseURL + restRequest.Resource);

    // Log headers
    Memo1.Lines.Add('Headers:');
    for item in restRequest.Params do
    begin
      Memo1.Lines.Add(item.Value);
    end;

    // 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);

  restClient.Free;
  restRequest.Free;
  restResponse.Free;

end;

procedure TfQB.getCompanyInfo();
var
  restClient: TRESTClient;
  restRequest: TRESTRequest;
  restResponse: TRESTResponse;
  param: TRESTRequestParameter;
  res: string;
  sid: string;
  jsValue: TJSONValue;
  jsObj, companyInfo: TJSONObject;
  jaCalls: TJSONArray;
  joCall: TJSONObject;
  row: integer;
  i: integer;
  uri, temp: string;
  sql: string;
  companyID, client, secret: string;
  pair: TJSONPair;
begin
  client := 'ABgO14uvjh8XqLud7spQ8lkb98AUpcdA7HbyMJfCAtl65sQ5yy';
  secret := 'bQ06TRemHeAGFzVHRaTUvUoBU9jpU9itK6MOMgqN';
  companyID := '9341454272655710';
  restClient := TRESTClient.Create(nil);
  restClient.BaseURL := 'https://sandbox-quickbooks.api.intuit.com';

  restRequest := TRESTRequest.Create(nil);
  restRequest.Client := restClient;

  restResponse := TRESTResponse.Create(nil);
  restRequest.Response := restResponse;


  restRequest.Method := rmGET;
  res := '/v3/company/9341454272655710/companyinfo/9341454272655710';
  restRequest.Resource := res;

  param := restRequest.Params.AddItem;
  param.Name := 'Authorization';
  param.Kind := pkHTTPHEADER;
  param.Options := param.Options + [TRESTRequestParameterOption.poDoNotEncode];
  param.Value := 'Bearer eyJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiZGlyIn0..jQN6f8KqIOzuz9vUug_-pQ.EmzmjomzD8h1L3jLN2-LnwbeegkYJw2WSqm5FFhXCTBGfaK9Hc16l_NuoKgesfYUNBMFuAO59wgc7bKni6QoCZmA6R40_RuC-fq7aSLms_JHLCvaWe-TiIY0iEDgtCgFP7Bfnw7VAP8QL-A8vkG0wVqFhrjLSbpZWIDMo9n_WG5U_tobHt052YKczM4A-wLtmouLnRNewvjk4YqU36867Z4fOZQ4c2AHm5yhA1CRfYNiKflT4Wv_ERMEG_A1U5FCj_vTs3njbjCrSxooKeW6yJy3pzbAv0eVH_0KFsAi5LjTfyDXPRfVa2XMjtJcfEKnFYvnFw4owOYP8z4TPVB2vNSeYSimHivOWlaWLFQzc1jbGgUpdeMdMqA9Wmpt0JgYZxCSMxDfTmSdYhDOR0ng6osf0qTaeSLsbweYMZRFZTZ1Ob8MNiAhTEPARTrJYgJ3YHdY1G_nX4-LhTJetkduiNzjK_LekBplwy0NnLAujqaiL0D6Wrwc-LNrYgQ4Qc3j46U4jGkOLJ88CIg85pDSAeFtZk5-vL8mRDpZ7s6KX62mIOpzdx_nTTeQgsTDhvUfusQcBshoEw93Tmv5s-tr96pSHXcCq4WLp6bRMsay65cEapQVE4GnZrXoWBwBh0cnpE6DdmXS2GQXXvW6fFsQcq8eTPyLGs-qR6nJL-yybGwI_XTmEsaYzbdT4cFFRRqTstbN_UWnv2BtIYOwgDPcOsBg8CKPa5ZZfJFunkrRI8HDLckgkSoLVMqXKzmMQleu.-tUa8rbIzo9JQylw3T_f1g';

  restRequest.Execute;

  //restresponse.Content
  //jsValue := restResponse.JSONValue;
  jsObj := TJSONObject.ParseJSONValue(restresponse.Content) as TJSONObject;
  companyInfo := TJSONObject(jsObj.GetValue('CompanyInfo'));

  for pair in companyInfo do
  begin
    // Get the value as string
    if pair.JsonValue is TJSONString then
      temp := TJSONString(pair.JsonValue).Value
    else
      temp := pair.JsonValue.ToString; // For non-string values

    // Add the key-value pair to the Memo
    Memo1.Lines.Add(pair.JsonString.Value + ': ' + temp);
  end;

  //temp := companyInfo.Values['CompanyName'].Value;

  //Memo1.Lines.Add(temp);

  i := 1;

  restClient.Free;
  restRequest.Free;
  restResponse.Free;

end;

end.
