使用 Google Apps 脚本和 Apps 脚本 OAuth 2.0 库从 Xero 的 API 获取数据

问题描述 投票:0回答:1

在运行 logAuthorizationUrl() 函数并使用记录的 Auth 进行身份验证后,运行下面的 getXeroAPI() 函数时,我不断收到 403 HTTP 错误。网址。

应用程序脚本

var OAUTH2_CLIENT_ID = '1EDE5A22F9094FE39DA662AB1121C941';
var OAUTH2_CLIENT_SECRET = 'MSmAeJrezgJiS4eu-qAIHle-k7_6Rs9VRJV555YaNGQVfBSN';
var OAUTH2_CALLBACK = 'https://script.google.com/macros/d/12s4fFSYPEGm4z6vQL_I0cn4Cs0mzpe60obLsQQ3D_1ilCspjrckz5ee8/usercallback';
var XERO_TENANT_ID = '600eb748-21b1-4737-8e50-f4ca6d7de9cc'; // Required for some API calls

/**
 * Creates an OAuth2 service for the Xero API.
 */
function getXeroService() {
  return OAuth2.createService('xero')
      // Set the endpoint URLs.
      .setAuthorizationBaseUrl('https://login.xero.com/identity/connect/authorize')
      .setTokenUrl('https://identity.xero.com/connect/token')

      // Set the client ID and secret.
      .setClientId(OAUTH2_CLIENT_ID)
      .setClientSecret(OAUTH2_CLIENT_SECRET)

      // Set the name of the callback function that should be invoked to complete
      // the OAuth flow.
      .setCallbackFunction('authCallback')

      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getUserProperties())

      // Set the scopes required for your application.
      .setScope('openid profile email accounting.transactions.read accounting.journals.read')
      
      // Set the grant type
      .setGrantType('authorization_code');
}


/**
 * Handles the OAuth callback.
 */
function authCallback(request) {
  var xeroService = getXeroService();
  var isAuthorized = xeroService.handleCallback(request);
  if (isAuthorized) {
    return HtmlService.createHtmlOutput('Success! You can close this tab.');
  } else {
    return HtmlService.createHtmlOutput('Denied. You can close this tab');
  }
}

/**
 * Logs the redirect URI to register in Xero app settings.
 */
function logRedirectUri() {
  var service = getXeroService();
  Logger.log(service.getRedirectUri());
}


/**
 * Checks if the service is authorized and logs the authorization URL if it's not.
 */
function checkAuth() {
  var service = getXeroService();
  if (!service.hasAccess()) {
    Logger.log('Authorize this script by visiting this url: ', service.getAuthorizationUrl());
  } else {
    Logger.log('The script is already authorized.');
  }
}

function logAuthorizationUrl() {
  var service = getXeroService();
  if (!service.hasAccess()) {
    var authorizationUrl = service.getAuthorizationUrl();
    Logger.log('Open the following URL and re-run the script: %s', authorizationUrl);
  }
}

/*
 * Example function that makes a call to Xero's API.

function getXeroData() {
  var service = getXeroService();
  if (service.hasAccess()) {
    var url = 'https://api.xero.com/api.xro/2.0/Journals'; // Example endpoint
    var response = UrlFetchApp.fetch(url, {
      headers: {
        'Authorization': 'Bearer ' + service.getAccessToken(),
        'xero-tenant-id': XERO_TENANT_ID
      }
    });
    var result = JSON.parse(response.getContentText());
    Logger.log(result);
  } else {
    Logger.log('The script is not yet authorized.');
  }
}
*/


function callXeroApi() {
  var service = getXeroService();
  if (service.hasAccess()) {
    var url = 'https://api.xero.com/api.xro/2.0/Journals';
    var options = {
      method: 'GET', // Explicitly set the HTTP request method to GET
      headers: {
        'Authorization': 'Bearer ' + service.getAccessToken(),
        'Accept': 'application/json'
      },
      muteHttpExceptions: true // Optional: Prevents exceptions for HTTP error responses (e.g., 404, 500)
    };
    var response = UrlFetchApp.fetch(url, options);
    
    // Check for success response code (e.g., 200 OK)
    if (response.getResponseCode() == 200) {
      var json = JSON.parse(response.getContentText());
      Logger.log(json);
    } else {
      Logger.log('Error fetching data from Xero API. Response code: ' + response.getResponseCode());
    }
  } else {
    Logger.log('No access token available.');
  }
}

在运行 logAuthorizationUrl() 函数并使用记录的 Auth 进行身份验证后,运行下面的 getXeroAPI() 函数时,我不断收到 403 HTTP 错误。网址。 我应该怎么做才能停止出现此错误?

在运行 logAuthorizationUrl() 函数并使用记录的 Auth 进行身份验证后,运行下面的 getXeroAPI() 函数时,我不断收到 403 HTTP 错误。网址。 我应该怎么做才能停止出现此错误?

api google-apps-script oauth-2.0 xero-api
1个回答
0
投票

我可以在这个线程中看到你有一个客户端 ID 和秘密;出于安全原因,请您编辑这篇文章以编辑它们,然后在developer.xero.com中生成一个新的秘密。

当您向期刊端点(和其他端点)发出请求时,请确保在请求标头中包含租户 ID,目前缺少此信息。

如果您仍有问题,可以使用此表格

向 Xero 提出案例吗
© www.soinside.com 2019 - 2024. All rights reserved.