Google App 外部 API 调用 box.com 使用 oauth2,找不到回调函数



我正在编写代码,以查找存储在某个用户 box.com 中的文件,获取有关这些文件的某些数据并将数据放入Google表格中。我的脚本在 Box 上进行身份验证,但在重定向 THE回调函数上找不到。

我得到的错误:

找不到脚本函数:回调

我在框配置中的重定向 URL 是:

https://script.google.com/macros/d/{MY-GOOGLE-APP-ID}/usercallback

代码如下:

var CLIENT_ID = 'MY-CLIENT-ID';
var CLIENT_SECRET = 'MY-CLIENT-SECRET';
function run() {
  console.log('function run()');
  var service = getService();
  if (service.hasAccess()) {
    console.log('service.hasAccess TRUE');
    var url = 'https://api.box.com/2.0/folders/0';
    var response = UrlFetchApp.fetch(url, {
      headers: {
        Authorization: 'Bearer ' + service.getAccessToken()
      }
    });
    var result = JSON.parse(response.getContentText());
    console.log(JSON.stringify(result, null, 2));
  } else {
    console.log('service.hasAccess FALSE');
    showSidebar();
  }
}
/**
 * Reset the authorization state, so that it can be re-tested.
 */
function reset() {
  var service = getService();
  service.reset();
}
/**
 * Configures the service.
 */
function getService() {
  console.log('function getService()');
  return OAuth2.createService('Box')
      // Set the endpoint URLs.
      .setAuthorizationBaseUrl('https://account.box.com/api/oauth2/authorize')
      .setTokenUrl('https://api.box.com/oauth2/token')
      // Set the client ID and secret.
      .setClientId(CLIENT_ID)
      .setClientSecret(CLIENT_SECRET)
      // Set the name of the callback function that should be invoked to
      // complete the OAuth flow.
      .setCallbackFunction('usercallback')
      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getUserProperties())
      // Set additional headers required
      .setParam('state', ScriptApp.newStateToken().createToken());
}
/**
 * Handles the OAuth callback.
 */
function usercallback(request) {
  console.log('function usercallback()');
  var service = getService();
  var authorized = service.handleCallback(request);
  if (authorized) {
    console.log('authorized TRUE');
    return HtmlService.createHtmlOutput('Success!');
  } else {
    console.log('authorized FALSE');
    return HtmlService.createHtmlOutput('Denied');
  }
}
function logRedirectUri() {
  var service = getService();
  Logger.log(service.getRedirectUri());
}
function showSidebar() {
  console.log('function showSidebar()');
  var service = getService();
  if (!service.hasAccess()) {
    console.log('service.hasAccess FALSE');
    var authorizationUrl = service.getAuthorizationUrl();
    var template = HtmlService.createTemplate(
        '<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>. ' +
        'Reopen the sidebar when the authorization is complete.');
    template.authorizationUrl = authorizationUrl;
    var page = template.evaluate();
    SpreadsheetApp.getUi().showSidebar(page);
  } else {
    console.log('service.hasAccess TRUE');
  // ...
  }
}

感谢这个页面 - https://ctrlq.org/code/20088-box-api-google-script 我能够确定我的脚本中最终出了什么问题。

当我从function getService()中删除.setParam('state', ScriptApp.newStateToken().createToken()); 时,它开始工作,我能够提取我期望的数据。

最新更新