2

Reading this doc from Salesforce, it was my understanding that a named credential handled authenticating when using it to make a callout. I'm going to go out on a limb and say that it probably does, I'm just doing something wrong. I set up a named credential for Quickbooks.

Named Credential

In order to create that Named Credential, I had to setup an Auth. Provider.

enter image description here

But then when I wrote this quick snippet of code,

public Integer getClientOutstandingBalance(Id persCustId) {
    String entId = [SELECT QB_Entity_Id__c FROM Account WHERE Id =: persCustId].QB_Entity_Id__c;

    if ( String.isEmpty(entId) ) return 0;

    String namedCredential = 'callout:Quickbooks';
    String custPath = '/v3/company/[redacted]/customer/4319';

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint(namedCredential+custPath);
    req.setMethod( 'GET' );

    HttpResponse resp = h.send(req);

    System.debug('#### resp status: '+resp.getStatus());
    System.debug('#### resp code: '+resp.getStatusCode());
    System.debug('#### resp: '+resp.getBody());

    return 0;
}

The response I get from Quickbooks is,

#### resp status: Unauthorized

#### resp code: 401

#### resp: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2018-10-03T11:23:32.531-07:00"><Fault type="AuthenticationFault"><Error code="100"><Message>General Authentication Error</Message><Detail>AuthenticationErrorGeneral: SRV-110-Authentication Failure , statusCode: 401</Detail></Error></Fault></IntuitResponse>

Is there something that I'm missing?

3

You will likely need to set an additional HTTP header with the OAuthToken . See Merge Fields for Apex Callouts That Use Named Credentials

Something like:

req.setHeader('Authorization', 'Bearer {!$Quickbooks.OAuthToken}'); 

Also, I see the named credential has an "Authentication Status" of "Pending". enter image description here

Try completing the OAuth process first. Usually you check "Start Authentication Flow on Save" when editing the Named Credential.

enter image description here

  • Thanks for the reply, I'm trying the tokens with the merge fields, per the doc you linked and am still getting the issue. I think you're closer with the, "pending" status. The start authentication flow box was checked when I first set it up, it's now been almost a day and still the status shows "pending". Is there somethign I did wrong setting it up? – Dan Oct 3 '18 at 20:32
  • I think the next problem is your Auth Provider "Authorize Endpoint URL" and "Token Endpoint URL". Your goal is to authenticate to Quickbooks, so I'd expect those URL's to be something provided by quickbooks. As they are now you are trying to authenticate back into a Salesforce sandbox. – Daniel Ballinger Oct 3 '18 at 20:39

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.