使用执行API PHP客户端创建文档时,Google Apps脚本错误

使用执行API PHP客户端创建文档时,Google Apps脚本错误

问题描述:

我尝试获取文件夹列表,并且它被php客户端成功获取. 但是,当我尝试使用应用程序脚本创建文档时,如果我使用应用程序脚本编辑器中的运行"图标运行应用程序脚本代码,则会创建该文档. 但是当我使用PHP客户端执行该方法时,它显示了一个错误.

I tried fetching the list of folder and it got successfully fetched by php client. But when i tried creating a document using app script, the document gets created if i run app script code using Run icon in app script editor. But when I am using PHP client to execute the method, it shows me an error.

捕获的异常:调用POST https://script.googleapis.com/v1/scripts/..................:run: (401) ScriptError

Caught exception: Error calling POST https://script.googleapis.com/v1/scripts/..................:run: (401) ScriptError

我也同时发送了两个范围:

Also i have send both scopes:

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;
}

问题出在我的代码中.我正在使用重定向URI,并且两个文件都需要权限范围. 但是我仅在一个文件中添加了权限范围. 因此,无论您在哪里请求客户端,都可以在所有文件中添加权限.

Problem was in my code. I was using redirect URI and the permission scope was needed in both files. But i added permission scope in only one file. So add permissions in all files wherever you are requesting the client.