Google 应用程序外部 API 使用 oauth2 调用 box.com,未找到回调函数
Google App external API call to box.com using oauth2, callback function not found
我正在编写代码,查找某个用户下存储在 box.com 中的文件,获取有关这些文件的某些数据并将数据放入 Google Sheet。我的脚本已达到在 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 我才能够确定我的脚本最终出了什么问题。
当我从 function getService()
中删除 .setParam('state', ScriptApp.newStateToken().createToken());
时,它开始工作并且我能够提取我期望的数据。
我正在编写代码,查找某个用户下存储在 box.com 中的文件,获取有关这些文件的某些数据并将数据放入 Google Sheet。我的脚本已达到在 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 我才能够确定我的脚本最终出了什么问题。
当我从 function getService()
中删除 .setParam('state', ScriptApp.newStateToken().createToken());
时,它开始工作并且我能够提取我期望的数据。