在Xero中使用OAuth2创建发票

时间:2019-12-03 01:32:24

标签: php oauth-2.0 xero-api

我将遵循此处提供的代码示例-https://github.com/XeroAPI/xero-php-oauth2/blob/master/docs/Api/AccountingApi.md#createInvoice

但是,我总是收到以下错误消息:

  

“ ErrorNumber”:17,

     

“类型”:“ NoDataProcessedException”,

     

“消息”:“此终结点尚未处理任何数据。   端点期望在请求中指定发票数据   身体。”

任何想法为何如此。

我在$ invoice数据周围添加了报价

以下代码:

<?php
ini_set('display_errors', 'On');
require 'vendor/autoload.php';
require_once('storage.php');

// Storage Classe uses sessions for storing token > extend to your DB of choice
$storage = new StorageClass();
$xeroTenantId = (string)$storage->getSession()['tenant_id'];

if ($storage->getHasExpired()) {
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
  'clientId'                => 'xxx',   
  'clientSecret'            => 'xxx-QOnb_kvBiQEb',
  'redirectUri'             => 'http://localhost/xero/callback.php',
  'urlAuthorize'            => 'https://login.xero.com/identity/connect/authorize',
  'urlAccessToken'          => 'https://identity.xero.com/connect/token',
  'urlResourceOwnerDetails' => 'https://api.xero.com/api.xro/2.0/Organisation'
]);

$newAccessToken = $provider->getAccessToken('refresh_token', [
  'refresh_token' => $storage->getRefreshToken()
]);

// Save my token, expiration and refresh token
$storage->setToken(
  $newAccessToken->getToken(),
  $newAccessToken->getExpires(), 
  $xeroTenantId,
  $newAccessToken->getRefreshToken()
);
}

$config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken( (string)$storage->getSession()['token'] );
$config->setHost("https://api.xero.com/api.xro/2.0");        

$apiInstance = new XeroAPI\XeroPHP\Api\AccountingApi(
  new GuzzleHttp\Client(),
  $config
);

$invoices = '{
            "Invoices": [{
              "Type": "ACCREC",
              "Contact": {
                "Name": "David Camerotto"
              },
              "LineItems": [{
                "Description": "Deposit for VBA Course",
                "Quantity": 1.0,
                "UnitAmount": 200.0,
                "AccountCode": "200",
                "TaxType": "NONE",
                "LineAmount": 200.0
              }],
              "Date": "2019-12-11",
              "DueDate": "2019-12-21",
              "Reference": "Website Design",
              "Status": "AUTHORISED"
            }]
          }';

$summarize_errors = True; 


try {
  $result = $apiInstance->createInvoice($xeroTenantId, $invoices, $summarize_errors);
  print_r($result);
} catch (Exception $e) {
  echo 'Exception when calling AccountingApi->createInvoice: ', $e->getMessage(), PHP_EOL;
  var_dump($e);
}



?>

1 个答案:

答案 0 :(得分:3)

如果再次查看示例,将会发现您错误地构建了$ invoice变量,因此,当SDK通过$ invoice变量将API发送给API时,它错误地进行了序列化,从而离开了请求正文无法被API使用。

在您的代码中,您将$ invoice变量构建为字符串,而示例将变量构建为对象。如果您尝试以相同的方式构建变量,则它应该可以工作。