0

I am working on integrating Salesforce with Quickbooks using Oauth 2.0. I am able to get Access token and Refresh token with the help of client_d and consumer_secret. The scope of access token is - com.intuit.quickbooks.accounting. The problem is when I use the fetched Access Token to make API request via Postman, it is working fine. But when I tried to make an API call from Apex class I get the following error:

General Authentication ErrorAuthenticationErrorGeneral: SRV-110-Authentication Failure , statusCode: 401

I am using following code to make the API call :

    String accessToken = '<access_token>';    
    String endPoint = 'https://sandbox-quickbooks.api.intuit.com/v3/company/<Sandbox_Id>/invoice/145?minorversion=4';
    HttpRequest httpReq = new HttpRequest();
    HttpResponse httpRes = new HttpResponse();
    Http http = new Http();
    httpReq.setMethod('GET');
    httpReq.setEndPoint(endPoint);
    httpReq.setHeader('Accept','*/*');
    httpReq.setHeader('Content-Type','application/json');
    httpReq.setHeader('charset','UTF-8');
    httpReq.setHeader('Authorization' , 'Bearer '+accessToken);
    httpRes = http.send(httpReq);

Can anyone please let me know what I am doing wrong. Thanks in advance.

PS: I have edited the code. The above code is working fine.

  • 401 Unauthorized - Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. 401 semantically means "unauthenticated", i.e. the user does not have the necessary credentials. Note: Some sites issue HTTP 401 when an IP address is banned from the website (usually the website domain) and that specific address is refused permission to access a website. – Adrian Larson Jan 29 '18 at 18:59
  • Are you sure your accessToken is valid? – Adrian Larson Jan 29 '18 at 19:00
  • Yes @Adrian , I am able to make API calls via POSTMAN using the same accessToken. So I guess the accessToken is valid :). – dev_skv Jan 29 '18 at 19:05
  • 3
    Compare what headers you are using in Postman to what you use in Apex. – Adrian Larson Jan 29 '18 at 19:08
  • @Adrian I am using the same headers. You can see the screenshot from postman here - goo.gl/ctB4TS – dev_skv Jan 29 '18 at 19:26
0

Got it.... I was passing Basic instead of Bearer in the Authorization header. I have edited to code snippet in my question!!

  • i understand your answer, however, you might want to include what you changed in your answer (code snippet), just for completeness sake =) . And you can then accept your answer (24-48 hrs i think) – glls Jan 30 '18 at 14:12
  • 1
    Better to leave the answer & let the OP self accept than to delete it & have the question float around the stack, getting ghost bumps from @commmunity. – battery.cord Jan 30 '18 at 14:41

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.