0

I am doing an integration of Quickbooks and Salesforce as 3-rd party solutions do not satisfy my requirements.

I created Quickbooks online developer account and have an app and Customer Key and Customer Secret. I want to write a trigger in Salesforce that would write values to the Quickbooks accounts, but can't get pass Authentication. I tried reading Quickbooks API but it is really confusing.

Basically, I am trying to HTTP to authenticate my app on Quickbooks withing an apex class. Following this OAuth tutorial led me to write this code:

global class AnimalsCallouts {

    @future (callout=true)
    public static void loginOauth() {

        string url = 'https://oauth.intuit.com/oauth/v1/get_request_token?';
        string body = buildRequestUrl();

        System.debug('Request Url + ' + body);

        // Build the request
        HttpRequest request = new HttpRequest();
        request.setEndPoint(url);

        request.setBody(body);

        request.setMethod('GET');

        // Send the request
        Http http = new Http();
        httpResponse response = http.send(request);
        string responseString = string.valueOf(response);
        System.debug('RESPONSE BODY + ' + response.getBody());
        System.debug('RESPONSE CODE + ' + response.getStatusCode());
        System.debug('RESPONSE STATUS + ' + response.getStatus());

    }

    public static string buildRequestUrl() {

        string requestUrl = '';

        string oauth_callback = 'oob';
        string encoded_oauth_callback = EncodingUtil.urlEncode('oob', 'UTF-8');
        string oauth_consumer_key = 'CONSUMER_KEY';
        string oauth_nonce = String.valueOf(Crypto.getRandomLong());  
        //string oauth_nonce = '123';
        string oauth_signature_method = 'HMAC-SHA1';
        string oauth_timestamp = string.valueOf(integer.valueOf(DateTime.now().getTime() / 1000.0));
        string oauth_version = '1.0';
        //string oauth_token_secret = '2CnBcbIEcyVx8pPFhqXfYlCtyXarGwRtTkRToRdl';        
        string signatureBaseString; // construct the base string
        string oauth_consumer_secret = '2CnBcbIEcyVx8pPFhqXfYlCtyXarGwRtTkRToRdl';
        string signingKey = oauth_consumer_secret + '&';

        signatureBaseString = 'GET' +
            '&https://oauth.intuit.com/oauth/v1/get_request_token' +
            '&oauth_callback=' + encoded_oauth_callback +
            '&oauth_consumer_key=' + oauth_consumer_key +
            '&oauth_nonce=' + oauth_nonce +
            '&oauth_signature_method=' + oauth_signature_method +
            '&oauth_timestamp=' + oauth_timestamp +
            '&oauth_version=' + oauth_version;

        System.debug('Base String: ' + signatureBaseString);

        signatureBaseString = EncodingUtil.urlEncode(signatureBaseString, 'UTF-8');
        System.debug('Base String: ' + signatureBaseString);
        Blob bSignatureBaseString = Blob.valueOf(signatureBaseString);
        Blob bSigningKey = Blob.valueOf(signingKey);
        Blob bSignature = Crypto.generateMac('hmacSHA1', bSignatureBaseString, bSigningKey);
        string oauth_signature = EncodingUtil.base64Encode(bSignature);

        string bodyString = '&oauth_callback=' + oauth_callback +
            '&oauth_consumer_key=' + oauth_consumer_key +
            '&oauth_nonce=' + oauth_nonce +
            '&oauth_signature_method=' + oauth_signature_method +
            '&oauth_timestamp=' + oauth_timestamp +
            '&oauth_version=' + oauth_version +
            '&oauth_signature=' + oauth_signature;

        return bodyString;

    }

}

I get this error:

[oauth_problem=signature_invalid] , error 401*

I'm not sure why my signature is not valid. Maybe URL or Server link is not valid or I got confused in base64 encoding somewhere.

2

I'm one of the developers of Breadwinner for QuickBooks Online, and I'm sorry our integration with Salesforce and QuickBooks Online didn't meet your needs. Feel free to contact our Sales team and they might be able to help.

In any case, you are URL encoding the signatureBaseString after adding GET&. & should not be encoded before oauth parameters. Base String should look something like

GET&https%3A%2F%2Foauth.intuit.com%2Foauth%2Fv1%2Fget_request_token& oauth_consumer_key%3Dqyprd46Is0FZ3v1tuE4unkw3iq6cUB%26oauth_nonce%3DX3e3aflZMeKPDwMI%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1398355877%26oauth_version%3D1.0

Also I can't see the Authorization header in your code.

These links would help you

  • It was the authorization header in the end, thanks for the assistance! – Alan Abishev Oct 27 '16 at 15:06

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.