Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用oauth2对box.com进行Google App外部API调用,未找到回调函数_Javascript_Google Apps Script_Oauth 2.0_Box - Fatal编程技术网

Javascript 使用oauth2对box.com进行Google App外部API调用,未找到回调函数

Javascript 使用oauth2对box.com进行Google App外部API调用,未找到回调函数,javascript,google-apps-script,oauth-2.0,box,Javascript,Google Apps Script,Oauth 2.0,Box,我正在编写代码,查找某个用户下存储在box.com中的文件,获取关于这些文件的特定数据,并将数据放在谷歌工作表中。我的脚本在框中进行身份验证,但在重定向上找不到回调函数 我得到的是一个错误: 未找到脚本函数:回调 配置框中我的重定向URL为: https://script.google.com/macros/d/{MY-GOOGLE-APP-ID}/usercallback 代码如下: var CLIENT_ID = 'MY-CLIENT-ID'; var CLIENT_SECRET = 'M

我正在编写代码,查找某个用户下存储在box.com中的文件,获取关于这些文件的特定数据,并将数据放在谷歌工作表中。我的脚本在框中进行身份验证,但在重定向上找不到回调函数

我得到的是一个错误:

未找到脚本函数:回调

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

  // ...
  }
}
多亏了这一页,我才能够确定我的脚本到底出了什么问题

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

那么你是说“usercallback”函数没有被调用?看看你,你所做的似乎很好。当然,仍在寻找-您是否在不使用侧杆的情况下进行了测试?为了进行测试,您可以复制并粘贴授权url,然后重新运行脚本,看看我是怎么想的。但这对我来说很新鲜,所以我可能错过了一些东西。谢谢你的关注。我来看看你的第二条评论,看看我能从你的建议中得出什么。@LJH-你在第二条评论中的建议与我的脚本的行为相同。