Re: [Less Accounting API] Curl Authentication issue

34 views
Skip to first unread message

Steven Bristol

unread,
Dec 14, 2012, 9:37:02 AM12/14/12
to less-acco...@googlegroups.com
On Thu, Dec 13, 2012 at 6:40 PM, Hunter <hun...@stonegiantlabs.com> wrote:
> Howdy,
>
> I'm trying to use curl to communication with the Less Accounting API, but
> i'm doing something wrong as I keep getting the error "Couldn't authenticate
> you". I created my api key in my LA account. Here is my code:
>
>> $less_api_key = "<key redacted>";
>>
>> $less_biz_name = "<biz subdomain>";
>>
>> $post_values = "";
>> $post_values .= "expense[title]=".$acct_info['payee']."&";
>> $post_values .= "expense[notes]=".$acct_info['description']."&";
>> $post_values .= "expense[amount]=".$acct_info['amount']."&";
>> $post_values .= "expense[paid_date]=".$acct_info['date']."&";
>> $post_values .= "expense[check_number]=".$acct_info['ref']."&";
>> $post_values .= "expense[is_paid]=0&"; //default to not paid
>> $post_values .= "api_key=".$less_api_key."";
>>
>>
>>
>> $post_url = "https://$less_biz_name.lessaccounting.com/expenses.xml";
>> $ch = curl_init($post_url);
>> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
>> curl_setopt($ch, CURLOPT_POSTFIELDS, $post_values);
>> curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
>> $result_page = curl_exec($ch);
>> curl_close($ch);
>> var_dump($result_page);
>
>
> Any help would be greatly appreciated!


You also have to supply your username and password and use basic auth.

cheers,
steven bristol

Hunter

unread,
Dec 14, 2012, 5:07:43 PM12/14/12
to less-acco...@googlegroups.com, st...@lesseverything.com
Okay thanks for the tip!

As reference for anyone else here is what i ended up with (first though I created a new, dedicated, less accounting user):

$less_api_key = "<key redacted>";
$less_biz_name = "<biz subdomain>";
$less_username = "<username>";
$less_password = "<password>";

$post_values = "";
$post_values .= "expense[title]=".$acct_info['payee']."&";
$post_values .= "expense[notes]=".$acct_info['description']."&";
$post_values .= "expense[amount]=".$acct_info['amount']."&";
$post_values .= "expense[paid_date]=".$acct_info['date']."&";
$post_values .= "expense[check_number]=".$acct_info['ref']."&";
$post_values .= "expense[is_paid]=0&";
$post_values .= "api_key=".$less_api_key."";
$post_url = "https://$less_biz_name.lessaccounting.com/expenses.xml";
$ch = curl_init($post_url);
curl_setopt($ch, CURLOPT_USERPWD, $less_username.':'.$less_password);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_values);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result_page = curl_exec($ch);
curl_close($ch);
var_dump($result_page);  

Hope that helps someone. Obviously you'll need to put in proper error checking etc.
Reply all
Reply to author
Forward
0 new messages