在 xero 中使用 OAuth2 创建发票 [英] creating an invoice using OAuth2 in xero

查看:92
本文介绍了在 xero 中使用 OAuth2 创建发票的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

错误编号":17,

类型":NoDataProcessedException",

"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.

我在 $invoice 数据周围添加了引号

I have added quotes around the $invoice data

代码如下:

<?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 通过 $将发票变量发送给它正在执行序列化的 API 错误地导致 API 无法使用请求正文.

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.

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

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.

这篇关于在 xero 中使用 OAuth2 创建发票的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆