我尝试获取文件夹列表,它被 php 客户端成功获取。但是当我尝试使用应用脚本创建文档时,如果我使用应用脚本编辑器中的运行图标运行应用脚本代码,则会创建文档。但是当我使用 PHP 客户端执行该方法时,它显示了一个错误。
捕获异常:调用 POST 时出错https://script.googleapis.com/v1/scripts/..................:run: (401) ScriptError
我也发送了两个范围:
https://www.googleapis.com/auth/documents https://www.googleapis.com/auth/drive
$client = new Google_Client();
$client->setApplicationName("Apps Script Execution");
$client->setScopes(array('https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/documents','https://www.googleapis.com/auth/drive.file'));
//$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
$client->setAuthConfigFile('client_secrets_app_script.json');
我该如何解决?
这是我的代码.gs
/**
* The function in this script will be called by the Apps Script Execution API.
*/
/**
* Return the set of folder names contained in the user's root folder as an
* object (with folder IDs as keys).
* @return {Object} A set of folder names keyed by folder ID.
*/
function getFoldersUnderRoot() {
var root = DriveApp.getRootFolder();
var folders = root.getFolders();
var folderSet = {};
while (folders.hasNext()) {
var folder = folders.next();
folderSet[folder.getId()] = folder.getName();
}
var doc = DocumentApp.create('Naya Doc');
var body = doc.getBody();
var rowsData = [['Plants', 'Animals'], ['Ficus', 'Goat'], ['Basil', 'Cat'], ['Moss', 'Frog']];
body.insertParagraph(0, doc.getName())
.setHeading(DocumentApp.ParagraphHeading.HEADING1);
table = body.appendTable(rowsData);
table.getRow(0).editAsText().setBold(true);
return folderSet;
}