用于公共类型应用程序的php中的xero api集成

时间:2014-10-08 06:33:02

标签: php codeigniter invoices xero-api

我想在php中将xero api集成到公共应用程序中。 我坚持使用oauth应用程序授权 我从github https://github.com/XeroAPI/XeroOAuth-PHP下载了代码(在xero api代码示例中找到公共应用程序)
我使用以下代码:

 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';

我正在传递以下xml数据:

$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];    
}    

首先我是oauth令牌,然后传入oauth invoice url

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

现在我收到401 error作为回应,oauth令牌不匹配

我在做什么错误?

1 个答案:

答案 0 :(得分:1)

如果您使用Xero收到OAuth错误,他们的OAuth Issues article有助于提供可能的解决方案。也就是说,&#34;令牌不匹配&#34;没有提到 - 我也找不到Xero社区中的错误参考。

根据您发布的内容,第一个问题是您是否已完成OAuth流程?有三个主要步骤(获取请求令牌,用户授权,获取访问令牌),上面的示例仅显示第一步。您引用的public.php文件包含所有步骤。

如果您确实让OAuth流程顺利运行,请确保OAuth访问令牌和机密与您的请求一起传递(只有令牌显示在您的示例请求中)。您可以在XeroOAuth对象中设置它们,因此最终请求可能看起来像

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

我根据XeroOauth-PHP public.php完成了基于OAuth和创建发票的完整流程a gist