问题
I am following along with the code samples provided here - https://github.com/XeroAPI/xero-php-oauth2/blob/master/docs/Api/AccountingApi.md#createInvoice
However, i am always receiving the following error message:
"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."
Any ideas why this is the case.
I have added quotes around the $invoice data
Code below:
<?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:
If you take a look at the example again you'll find that you're building up your $invoice variable incorrectly and so when the SDK sends through the $invoice variable to the API it's doing its serialization incorrectly leaving the request body unusable by the API.
In your code you're building the $invoice variable as a string whereas the example is building the variable as an object. If you try build your variable in the same way, it should work.
来源:https://stackoverflow.com/questions/59149006/creating-an-invoice-using-oauth2-in-xero