xero api integration in php for a public type application
Asked Answered
S

2

14

I want to integrate xero api for public application in php. I am stuck with oauth application authorization I have download code from github https://github.com/XeroAPI/XeroOAuth-PHP (find on xero api code sample for public application)
I am using following code:

 require('/../lib/XeroOAuth.php');    
    require('/../_config.php');    
    $useragent = "Xero-OAuth-PHP Public";    
    $signatures = array (
            'consumer_key' => 'app_consumre_key',
            'shared_secret' => 'app_secret_key',
            'core_version' => '2.0'
    );    
    $XeroOAuth = new XeroOAuth ( array_merge ( array (
            'application_type' => XRO_APP_TYPE,
            'oauth_callback' => OAUTH_CALLBACK,
            'user_agent' => $useragent 
    ), $signatures ) );    
    include 'tests.php';

I am passing following xml data:

$xml = "<Invoices>    
<Invoice>    
<Type>ACCREC</Type>    
<Contact>        
<Name>Martin Hudson</Name>        
</Contact>        
<Date>2013-05-13T00:00:00</Date>        
<DueDate>2013-05-20T00:00:00</DueDate>    
<LineAmountTypes>Exclusive</LineAmountTypes>    
<LineItems>    
<LineItem>    
<Description>Monthly rental for property at 56a Wilkins Avenue</Description>    
<Quantity>4.3400</Quantity>    
<UnitAmount>395.00</UnitAmount>    
<AccountCode>200</AccountCode>    
</LineItem>    
</LineItems>    
</Invoice>    
</Invoices>";    
$params = array (
                'oauth_callback' => OAUTH_CALLBACK 
);    
$response1 = $XeroOAuth->request ( 'GET', $XeroOAuth->url ( 'RequestToken', '' ), $params     );    
if ($XeroOAuth->response ['code'] == 200)    
{    
   $outhtoken = $XeroOAuth->response ['response'];    
   $oauth_exp = explode('&',$outhtoken);    
   $oauth_exp_token = explode('=',$oauth_exp[1]);    
   $oauth_token = $oauth_exp_token[1];    
}    

First I am oauth token, and passing into oauth invoice url

$response = $XeroOAuth->request('POST', $XeroOAuth->url('Invoices', 'core'),  array('oauth_token'=>$oauth_token), $xml);    

Now I am getting 401 error in response, oauth token mismatch

What mistake I am doing?

Stoa answered 8/10, 2014 at 6:33 Comment(1)
I suggest using this library instead. It's actually actively maintained, well structured, easy to debug and the author actually listens: github.com/calcinai/xero-php The official php library provided by xero is... bites tongue ...not good.Fervent
K
1

If you are getting OAuth errors with Xero, their OAuth Issues article is helpful to provide a possible solution. That said, "token mismatch" isn't mentioned - nor could I find reference to the error in the Xero community.

Based on what you've posted, the first question is if you've completed the OAuth process? There are three main steps (get a request token, user authorisation, get an access token) and your example above only shows the first step. The public.php file you've referenced contains all the steps.

If you do have the OAuth process running smoothly, then make sure the OAuth access token and secret are being passed with your request (only the token is shown in your sample request). You can set these in the XeroOAuth object, so the final request could look like

$XeroOAuth->config ['access_token'] = $oauth_token;
$XeroOAuth->config ['access_token_secret'] = $oauth_token_secret; 
$XeroOAuth->request('POST', $XeroOAuth->url('Invoices', 'core'),  array(), $xml);

I've made a gist with the complete process based off the XeroOauth-PHP public.php that demonstrates both OAuth and creating an invoice.

Kaka answered 1/12, 2014 at 8:5 Comment(0)
W
0

Xero is no longer supporting oAuth 1.0 Old Xero Github Repository

and you must have to use latest Git hub Repo with Xero OAuth 2.0 https://github.com/XeroAPI/xero-php-oauth2

Wells answered 5/4, 2022 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.