2014-10-08 71 views
14

我想在PHP中集成公共應用程序xero api。 我堅持的OAuth應用程序授權 我從GitHub https://github.com/XeroAPI/XeroOAuth-PHP下載代碼(找到公共應用Xero的API代碼示例)
我使用下面的代碼:xero api集成在php中用於公共類型應用程序

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的發票網址

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

現在我得到401 error作爲迴應,oauth令牌不匹配

我在做什麼錯誤?

+2

我建議使用這個庫來代替。它實際上是積極維護,結構良好,易於調試,作者實際上聽:https://github.com/calcinai/xero-php 由xero提供的官方php庫是... *咬舌​​頭* ...不好。 – Gerry 2015-07-13 04:17:37

回答

1

如果您使用Xero獲得OAuth錯誤,那麼它們的OAuth Issues article有助於提供可能的解決方案。也就是說,不提及「令牌不匹配」 - 也不能在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); 

我做了a gist基於關閉XeroOauth-PHP public.php一個演示這的OAuth和創建發票的完整過程。

相關問題