【问题标题】:Google App external API call to box.com using oauth2, callback function not found使用 oauth2 对 box.com 的 Google App 外部 API 调用,未找到回调函数
【发布时间】:2017-05-27 03:06:19
【问题描述】:

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

我得到的错误:

找不到脚本函数:回调

我在盒子配置中的重定向 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');

  // ...
  }
}

【问题讨论】:

  • 所以你是说'usercallback'函数没有被调用?看着github.com/googlesamples/apps-script-oauth2,你所做的似乎不错。当然还在寻找 ;-)
  • 你有没有在不使用侧栏的情况下进行测试?为了测试,您可以复制并粘贴授权 url 并重新运行脚本,请参阅github.com/googlesamples/apps-script-oauth2/blob/master/samples/…
  • 在我看来就是这样。但这对我来说很新,所以我可能会遗漏一些东西。感谢您的关注。我会看看你的第二条评论,看看我能从你的建议中找出什么。
  • @LJH - 您在第二条评论中的建议与我的脚本具有相同的行为。

标签: javascript google-apps-script oauth-2.0 box


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多