Oauth 2.0 Xero OAuth2问题

Oauth 2.0 Xero OAuth2问题,oauth-2.0,xero-api,xero,Oauth 2.0,Xero Api,Xero,我已经将我的网站迁移到Xero2.0,它可以用于创建发票。然而,几个小时后,我必须通过点击浏览器中的文件重新授权,重新连接到Xero帐户,否则我的客户会看到类似于下面的内容 致命错误:Uncaught BadMethodCallException:未传递必需参数:在/var/www/vhosts/something.com/httpdocs/xero-oauth2/vendor/league/oauth2 client/src/Tool/RequiredParameterTrait.php:35

我已经将我的网站迁移到Xero2.0,它可以用于创建发票。然而,几个小时后,我必须通过点击浏览器中的文件重新授权,重新连接到Xero帐户,否则我的客户会看到类似于下面的内容

致命错误:Uncaught BadMethodCallException:未传递必需参数:在/var/www/vhosts/something.com/httpdocs/xero-oauth2/vendor/league/oauth2 client/src/Tool/RequiredParameterTrait.php:35堆栈跟踪:#0/var/www/vhosts/something.com/httpdocs/xero-oauth2/vendors/league/oauth2/oauth2/client/src/Tool/RequiredParameterTrait.php(53):League\OAuth2\Client\Grant\AbstractGrant->checkRequiredParameter('refresh_token',Array)#1/var/www/vhosts/naschools.org.uk/httpdocs/xero-OAuth2/vendor/League/OAuth2 Client/src/Grant/AbstractGrant.php(76):League\OAuth2\Client\Grant\AbstractGrant->->checkRequiredParameters(Array,Array)#2/var/www/vhosts/something.com/httpdocs/xero-oauth2/vendor/league/oauth2 client/src/Provider/AbstractProvider.php(535):league\oauth2\client\Grant\AbstractGrant->prepareRequestParameters(Array,Array)#3/var/www/vhosts/something.com/httpdocs/xero-oauth2/createInvoice.php(160):league\oauth2\client\Provider\AbstractProvider\getAccessToken->(对象(第35行的/var/www/vhosts/something.com/httpdocs/xero-OAuth2/vendor/League/OAuth2-Client/src/Tool/RequiredParameterTrait.php中的League\OAuth2\Client\Grant\Refre)

这有什么明显的问题吗

            <?php 

            $storage = new StorageClass();
            $xeroTenantId = (string)$storage->getSession()['tenant_id'];

            if ($storage->getHasExpired()) {
                $provider = new \League\OAuth2\Client\Provider\GenericProvider([
                    'clientId' => 'XXXXXX',
                    'clientSecret' => 'XXXXXX',
                    'redirectUri' => 'https://something.com/xero-oauth2/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(),
                    $newAccessToken->getValues()["id_token"]);
            }

            // Configure OAuth2 access token for authorization: OAuth2
            $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
            );

            $xero_tenant_id = $xeroTenantId; // string | Xero identifier for Tenant

            // \XeroAPI\XeroPHP\Models\Accounting\Invoices | Invoices with an array of invoice objects in body of request
            $summarize_errors = true; // bool | If false return 200 OK and mix of successfully created objects and any with validation errors
            $unitdp = 4; // int | e.g. unitdp=4 – (Unit Decimal Places) You can opt in to use four decimal places for unit amounts


            $purchaseNumber = str_replace("&", "&amp;", $_SESSION['purchasenumber']);
            $schoolOrGname = str_replace("&", "&amp;", $_SESSION['schoolorgname1']);
            $billingEmail = str_replace("&", "&amp;", $_SESSION['billingemail']);
            $billingAddress = str_replace("&", "&amp;", $_SESSION['billingaddress']);
            $billingCity = str_replace("&", "&amp;", $_SESSION['billingcity']);
            $billingPostalCode = str_replace("&", "&amp;", $_SESSION['billingpostcode']);
            $billingFullName = str_replace("&", "&amp;", $_SESSION['billingfullname']);
            $date = str_replace("&", "&amp;", $_SESSION['now']);
            $dueDate = str_replace("&", "&amp;", $_SESSION['thirty']);
            $eventTitle = str_replace("&", "&amp;", $_SESSION['eventtitle']);
            $eventPrice = str_replace("&", "&amp;", $_SESSION['eventprice']);


            $address = new Address();
            $address->setAddressType('POBOX');
            $address->setAddressLine1($billingAddress);
            $address->setCity($billingCity);
            $address->setPostalCode($billingPostalCode);
            $address->setAttentionTo($billingFullName);

            $contact = new Contact();
            $contact->setName($schoolOrGname)
                ->setContactStatus('ACTIVE')
                ->setEmailAddress($billingEmail)
                ->setAddresses([$address]);

            $lineItem = new LineItem();
            $lineItem->setDescription($eventTitle)
                ->setQuantity(1)
                ->setAccountCode(4002)
                ->setUnitAmount($eventPrice)
                ->setTaxAmount(0)
                ->setTaxType('NONE');

            $invoice = new Invoice();
            $invoice->setDate($date)
                ->setDueDate($dueDate)
                ->setLineAmountTypes('Exclusive')
                ->setType('ACCREC')
                ->setReference($_SESSION['purchasenumber'])
                ->setStatus('AUTHORISED')
                ->setContact($contact)
                ->setLineItems([$lineItem]);

            try {
                $result = $apiInstance->createInvoices($xero_tenant_id, $invoice, $summarize_errors, $unitdp);
                header("Location: https://something.com/order-confirmation/");
            } catch (Exception $e) {

                print_r($e);
                echo '<br/><br/>Exception when calling AccountingApi->createInvoices: ', $e->getMessage(), PHP_EOL;
            }
            ?>

用户创建令牌后,您似乎只需在使用前刷新令牌即可。access\u令牌仅持续30分钟。每次使用前,您都需要刷新(并替换)令牌。您使用的是SDK,因此易于支持


自述文件中有一些示例代码向您展示了如何避免以下错误:

  • 为授权配置OAuth2访问令牌:OAuth2

主要的一点是确保在进行调用之前将刷新的令牌集替换到api客户端上。是否确定在配置和记帐客户端上正确设置了该令牌集

$config=XeroAPI\xerohp\Configuration::getDefaultConfiguration()->setAccessToken((字符串)$storage->getSession()['token']);

用户创建令牌后,您似乎只需在使用前刷新令牌即可。access\u令牌仅持续30分钟。每次使用前,您都需要刷新(并替换)令牌。您使用的是SDK,因此易于支持


自述文件中有一些示例代码向您展示了如何避免以下错误:

  • 为授权配置OAuth2访问令牌:OAuth2

主要的一点是确保在进行调用之前将刷新的令牌集替换到api客户端上。是否确定在配置和记帐客户端上正确设置了该令牌集

$config=XeroAPI\xerohp\Configuration::getDefaultConfiguration()->setAccessToken((字符串)$storage->getSession()['token']);