0

I am trying to setup a connection between Xero and Salesforce using oAUth2.0.

I am doing it as per the Xero Documentation mentioned here: https://developer.xero.com/documentation/oauth2/auth-flow

Step 1: To get Authorization Code which is working fine.
Step 2: Make a Post Request to Xero to get access token.

Here is my code:

HttpRequest request = new HttpRequest();
request.setEndpoint('https://identity.xero.com/connect/token');
request.setMethod('POST');
request.setHeader('grant_type','authorization_code');
request.setHeader('Authorization',authorizationHeader);
request.setHeader('redirect_uri','my redirect url');
request.setHeader('Content-Type','application/x-www-form-urlencoded');
request.setHeader('code',authCode);

When i am executing it, i am getting 411 as status code and message Length Required. I have tried to resolve the issue by adding request.setHeader('Content-Length','32') but now it's throwing Read Timeout issue.

Am i missing something?

Thanks, Soumen

2

The body of the request should contain the three elements listed as being in the body. They are not headers.

String body = 'grant_type=authorization_code&code='+authCode+'&redirect_uri=my redirect url';
request.setBody(body);
request.setHeader('Content-Length',body.length());

Note that the Read Timeout error happened because the Content-Length did not match the body's length (which was 0), so the server timed out waiting for the "rest" of the message that was never sent.

| improve this answer | |
  • Thank You @sfdcfox – Salesforce Development Dec 2 '19 at 19:56
  • @SalesforceDevelopment How do you get the authorization code from salesforce. can you please share the code for the same? – Aman Jain Jun 10 at 15:33
  • @AmanJain The connection I have established is from Salesforce to Xero. So The Authentication Code, Access Token, Refresh Token everything Xero is sending based on valid request. Let me know if you are looking for that. – Salesforce Development Jun 11 at 17:54
  • @SalesforceDevelopment, Thanks for the response! I was able to achieve it. I was concerned about how to make the request to get the auth token. Using the vf page i am able to achieve it. – Aman Jain Jun 12 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.