Jump to content
Sign in to follow this  
misc_bb

Xero API with Delphi Datasnap REST

Recommended Posts

We are building a Xero API with OAuth2 using the REST components in Delphi Datasnap. We have been following this Xero API flow: https://developer.xero.com/documentation/oauth2/auth-flow but we are encountering issues on Step 3. It gives me all the time an error of 'invalid_client' but when I tested the values in Postman, I was able to retrieved what is expected successfully with no errors. Here is part of the code. 

  RESTClient.BaseURL := 'https://identity.xero.com/';
  RESTClient.ContentType := 'application/x-www-form-urlencoded';

  RESTRequest.Resource := 'connect/token';


  RESTRequest.Params.AddItem('Authorization', Authorization, TRESTRequestParameterkind.pkHTTPHEADER);
  RESTRequest.Params.AddItem('grant_type', 'authorization_code', TRESTRequestParameterKind.pkREQUESTBODY);
  RESTRequest.Params.AddItem('code', sAuthCode, TRESTRequestParameterKind.pkREQUESTBODY);
  RESTRequest.Params.AddItem('redirect_uri', ReDirectURI, TRESTRequestParameterKind.pkREQUESTBODY);
  
  RESTRequest.Method := TRESTRequestMethod.rmPOST;
  RESTRequest.Execute;
  

I am suspecting it was the base64encoding part, but I was able to correct that one and I was able to successfully test the values in Postman. Is there a way, I can retrieve the full URI or REST parameters that is being sent by this component? I want to see is the entire value that is being sent by the RESTRequest. Is this possible? 

 

Thank you!

  • Thanks 1

Share this post


Link to post

After careful review I think REST component doesn't have any option to get the entire header and parameters. Although I was able to get my code working. So, I'll just share to you. 

  RESTClient.BaseURL := 'https://identity.xero.com/';
  RESTClient.ContentType := 'application/x-www-form-urlencoded';
  RESTClient.Params.AddItem('Authorization', Authorization, TRESTRequestParameterkind.pkHTTPHEADER, [TRESTRequestParameterOption.poDoNotEncode]);
  RESTRequest.Resource := 'connect/token';

  RESTRequest.Params.AddItem('grant_type', 'authorization_code', TRESTRequestParameterKind.pkREQUESTBODY);
  RESTRequest.Params.AddItem('code', sAuthCode, TRESTRequestParameterKind.pkREQUESTBODY);
  RESTRequest.Params.AddItem('redirect_uri', ReDirectURI, TRESTRequestParameterKind.pkREQUESTBODY);

  Logfile('authorization:' + Authorization + '&grant_type:authorization_code' + '&code:' + sAuthCode + '&redirect_uri:' + ReDirectURI);
  RESTRequest.Method := TRESTRequestMethod.rmPOST;
  RESTRequest.Execute;

I'm not exactly sure what happen in my previous code but I did transfer the header parameter in the RESTClient instead of placing it in RESTRequest and added the Restrequestparameteroption. Take note also that the Authorization here has a base64 encoding as specified by Xero but during the Delphi encoding it added a linefeed which was also one of the reasons why I keep getting a bad request but I had to remove manually before passing it here.

  tempFile := 'Basic ' + X.UrlEncode(sClientID + ':' + sClientSecret);
  sAuthorization := StringReplace(tempFile, #$D#$A, '', [rfReplaceAll]);

 

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×