user2029890
user2029890

Reputation: 2713

How to authenticate to Quickbooks Online API using CURL & PHP

I normally use the PHP SDK which works well. However, I need to call a resource that is not currently available via the SDK which is the ability to pull in a PDF invoice as per https://developer.intuit.com/docs/api/accounting/invoice

I therefore need to connect via the base URL. Now, this is an app I'm using just to connect to my own company account. I generated all the credentials via the oAuth Playground and manually renew every 180 days. This is what I tried:

$url = "https://quickbooks.api.intuit.com/v3/company/123456/invoice/8661/pdf";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("OAuth   oauth_token:*******", 
                                            "oauth_nonce:******",
                                            "oauth_consumer_key:*****", 
                                            "oauth_signature_method:HMAC-SHA1",
                                            "oauth_timestamp:1461326602",
                                            "oauth_version:1.0",
                                            "oauth_signature:******"));
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
$result = curl_exec ($curl);
print $result;

This is the response:

< HTTP/1.1 400 Illegal character 0x20
< Server: nginx
< Date: Fri, 22 Apr 2016 11:58:59 GMT
< Content-Length: 0
< Connection: keep-alive
< Keep-Alive: timeout=5

I don't even know if I'm using all the correct keys and tokens. I used the same values I successfully used for the PHP SDK to work.

I'd appreciate if someone can advise me what I'm doing wrong. Thanks.

Upvotes: 0

Views: 2394

Answers (2)

Kavi Joshi
Kavi Joshi

Reputation: 193

Maybe this will help you:

Create string with all parameters and keys, and make array with Authorization key and use in header part.

$auth = 'realm="123456",oauth_consumer_key="KEYSTRING",oauth_token="TOKENSTRING",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1502970860",oauth_nonce="KbSwZN",oauth_version="1.0",oauth_signature="W3fYzXn5EZ1ajo6SfU0LZEQPKfc%3D"';

$headr = array();
$headr[] = 'Authorization: OAuth '.$auth;

$url = "https://quickbooks.api.intuit.com/v3/company/123456/customer/2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
$response = curl_exec($ch);

echo "<pre>"; print_r($response); echo "</pre>"; die();

Upvotes: 0

Keith Palmer Jr.
Keith Palmer Jr.

Reputation: 27952

You have to sign your requests using OAuth, per the docs:

https://developer.intuit.com/docs/0100_accounting/0060_authentication_and_authorization

If you want to do this with cURL, you have to implement the OAuth spec:

http://oauth.net/core/1.0a/

Just hard-coding it as you've done in your example code will not work. You have to calculate a OAuth signature for every single request using the OAuth signing algorithm.

If you don't want to do this yourself, try using a library that already provides this functionality for you. For example, a OAuth library:

http://php.net/manual/en/book.oauth.php

Or an actual QuickBooks Online + PHP lib, that provides the functionality you need (disclaimer -- I'm the author):

https://github.com/consolibyte/quickbooks-php

Specifically, downloading PDFs:

Upvotes: 1

Related Questions