我正在编写代码,用于查找某个用户下存储在 box.com 中的文件,获取有关这些文件的某些数据并将这些数据放入 Google 表格中。我的脚本已经在 Box 上进行了身份验证,但在重定向时找不到回调函数。
我得到的错误:
Script function not found: callback
我在盒子配置中的重定向 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我能够确定我的脚本中最终出了什么问题。
当我删除 .setParam('state', ScriptApp.newStateToken().createToken()); 时
来自 function getService()
它开始工作,我能够提取我期望的数据。
关于javascript - Google App使用oauth2对box.com进行外部API调用,未找到回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44205765/