将XERO与PHP连接并获取发票

时间:2020-06-08 06:34:32

标签: php xero-api

这是我用于将XERO与PHP连接的代码:

ini_set('display_errors', 'On');
      require  './vendor/autoload.php';

      session_start();

      $provider = new \League\OAuth2\Client\Provider\GenericProvider([
        'clientId'                => 'clientid',   
        'clientSecret'            => 'clientsecret',
        'redirectUri'             => 'http://localhost/crm/',
        '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'
      ]);

      // Scope defines the data your app has permission to access.
      // Learn more about scopes at https://developer.xero.com/documentation/oauth2/scopes
      $options = [
          'scope' => ['openid email profile offline_access accounting.settings accounting.transactions accounting.contacts accounting.journals.read accounting.reports.read accounting.attachments']
      ];

      // This returns the authorizeUrl with necessary parameters applied (e.g. state).
      $authorizationUrl = $provider->getAuthorizationUrl($options);

      // Save the state generated for you and store it to the session.
      // For security, on callback we compare the saved state with the one returned to ensure they match.
      $_SESSION['oauth2'] = $provider->getState();

      $config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken($_SESSION['oauth2'] );     

          $apiInstance = new XeroAPI\XeroPHP\Api\AccountingApi(
            // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
            // This is optional, `GuzzleHttp\Client` will be used as default.
            new GuzzleHttp\Client(),
            $config
        );

        $xero_tenant_id = '4b55fe63-4d14-4e79-a6d7-62cd3ed12ffe'; // string | Xero identifier for Tenant
        $invoice_id = '50b74987-a7b8-436b-9570-b6810b88a965'; // string | Unique identifier for an Invoice
        $unitdp = 4; // int | e.g. unitdp=4 – (Unit Decimal Places) You can opt in to use four decimal places for unit amounts
        $result = $apiInstance->getInvoice($xero_tenant_id, $invoice_id, $unitdp);
            print_r($result);die;
        try {
            $result = $apiInstance->getInvoice($xero_tenant_id, $invoice_id, $unitdp);
            print_r($result);
        } catch (Exception $e) {
            echo 'Exception when calling AccountingApi->getInvoice: ', $e->getMessage(), PHP_EOL;
        }

我正在使用XERO API。我的目标是将发票详细信息从XERO帐户获取到我的Web应用程序。 我尝试了很多事情,但最终还是遇到了这段代码。 我还连接了XERO Accounting API以获取特定发票的发票详细信息。如您在代码中所见,还有我的发票ID。我在

中输入$_SESSION['oauth2']
XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken($_SESSION['oauth2'] );

,但在此之前,它要求access_token是这样的:

XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken(access_token);

但是我想我正在其中参加会议,这是错误的。 我试图生成访问令牌,但是无法执行,并且在运行代码时出现此错误:

401 AuthenticationUnsuccessful

0 个答案:

没有答案