在 xero 中使用 OAuth2 创建发票
creating an invoice using OAuth2 in xero
我正在关注此处提供的代码示例 - https://github.com/XeroAPI/xero-php-oauth2/blob/master/docs/Api/AccountingApi.md#createInvoice
但是,我总是收到以下错误消息:
"ErrorNumber": 17,
"Type": "NoDataProcessedException",
"Message": "No data has been processed for this endpoint. This
endpoint is expecting Invoice data to be specifed in the request
body."
知道为什么会这样。
我在 $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->createInvoices($xeroTenantId, $invoices, $summarize_errors);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling AccountingApi->createInvoice: ', $e->getMessage(), PHP_EOL;
var_dump($e);
}
?>
如果您再次查看该示例,您会发现您构建的 $invoice 变量不正确,因此当 SDK 通过 $invoice 变量发送到 API 时,它正在执行它的操作序列化错误地使 API.
无法使用请求正文
在您的代码中,您将 $invoice 变量构建为字符串,而该示例将变量构建为对象。如果您尝试以相同的方式构建您的变量,它应该可以工作。
我正在关注此处提供的代码示例 - https://github.com/XeroAPI/xero-php-oauth2/blob/master/docs/Api/AccountingApi.md#createInvoice
但是,我总是收到以下错误消息:
"ErrorNumber": 17,
"Type": "NoDataProcessedException",
"Message": "No data has been processed for this endpoint. This endpoint is expecting Invoice data to be specifed in the request body."
知道为什么会这样。
我在 $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->createInvoices($xeroTenantId, $invoices, $summarize_errors);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling AccountingApi->createInvoice: ', $e->getMessage(), PHP_EOL;
var_dump($e);
}
?>
如果您再次查看该示例,您会发现您构建的 $invoice 变量不正确,因此当 SDK 通过 $invoice 变量发送到 API 时,它正在执行它的操作序列化错误地使 API.
无法使用请求正文在您的代码中,您将 $invoice 变量构建为字符串,而该示例将变量构建为对象。如果您尝试以相同的方式构建您的变量,它应该可以工作。