Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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
Api 如何通过应用程序脚本创建新脚本文件&;驱动器SDK_Api_Google Apps Script_Google Drive Api - Fatal编程技术网

Api 如何通过应用程序脚本创建新脚本文件&;驱动器SDK

Api 如何通过应用程序脚本创建新脚本文件&;驱动器SDK,api,google-apps-script,google-drive-api,Api,Google Apps Script,Google Drive Api,正在尝试通过调用应用程序脚本中的驱动器SDK来创建包含文件的新项目 以下内容在UrlFetchApp请求中的具体位置 { "files": [ { "id":"9basdfbd-749a-4as9b-b9d1-d64basdf803", "name":"Code", "type":"server_js", "source":"function doGet() {\n return HtmlService.createHtmlOutpu

正在尝试通过调用应用程序脚本中的驱动器SDK来创建包含文件的新项目

以下内容在
UrlFetchApp
请求中的具体位置

{
  "files": [
    {
      "id":"9basdfbd-749a-4as9b-b9d1-d64basdf803",
      "name":"Code",
      "type":"server_js",
      "source":"function doGet() {\n  return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
    },
    {
      "id":"3asf7c0d-1afb-4a9-8431-5asdfc79e7ae",
      "name":"index",
      "type":"html",
      "source":"\u003chtml\u003e\n  \u003cbody\u003e\n    New message!!\n  \u003c/body\u003e\n\u003c/html\u003e"
    }
  ]
}
它来自,Dan提到调用的是应用程序脚本之外的语言,但在使用Eric的oAuth2库设置授权后,请求提供脚本文件类型列表和这些文件的内容

我最近的猜测

函数createProject(){
var token=getDriveService().getAccessToken();//来自Eric的oAuth2库
var url='1〕https://www.googleapis.com/upload/drive/v2/files?convert=true';
//这个去哪儿了?
变量文件={
“文件”:[
{
“名称”:“代码”,
“类型”:“服务器”,
“源”:“函数doGet(){\n返回HtmlService.createHtmlOutFromFile(\u0027index\u0027);\n}\n”
},
{
“名称”:“索引”,
“类型”:“html”,
“来源”:“\u003chtml\u003e\n\u003cbody\u003e\n你好,世界!!\n\u003c/body\u003e\n\u003c/html\u003e”
}
]
};
//这也到哪里去了?
变量元数据={
“title”:“script-test1”,
“mimeType”:“application/vnd.google apps.script”,
“父母”:[
{
“id”:“0B2VkNbQMTnaCODBRVjZQcXBXckU”
}
],
};
变量选项={
“标题”:{
“授权”:“持票人”+代币,
“内容类型”:“application/vnd.google apps.script+json”,
},
'method':'POST',
“有效负载”:文件//可能不正确
};
var response=UrlFetchApp.fetch(url,选项);
Logger.log(response.getResponseCode());
}
会创建未知类型的无标题驱动器文件,并将有效负载插入其中,但不会将其转换为脚本文件类型

走另一条路,只是使用

var file = {
    "title": "Test script",
    "mimeType": "application/vnd.google-apps.script",
    "parents": [
      {
        "id": "[INSERT FOLDER ID HERE]"
      }
    ]
  };

Drive.Files.insert(file);
…引发内部错误


也知道我们有一个客户端JS示例,但不知道其中有多少应该转换为服务器端应用程序脚本(如果可能)。

您几乎做到了,但有一些错误。您的第一种方法(使用
UrlFetch
)在
options
变量中有一些错误。下面是一个doGet()函数,它(在验证授权后)创建一个新的应用程序脚本项目,并将UrlFetch响应写入页面:

function doGet() {
  var driveService = getDriveService();
  if (!driveService.hasAccess()) {
    var authorizationUrl = driveService.getAuthorizationUrl();
    var template = HtmlService.createTemplate(
        '<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>. ' +
        'Refresh the page when authorization complete.');
    template.authorizationUrl = authorizationUrl;
    var page = template.evaluate();
    return page;
  } else {     
    var url = "https://www.googleapis.com/upload/drive/v2/files?convert=true";   
    var requestBody =  {
      "files": [
        {
          "name":"Code",
          "type":"server_js",
          "source":"function doGet() {\n  return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
        },
        {
          "name":"index",
          "type":"html",
         "source":"\u003chtml\u003e\n  \u003cbody\u003e\n    Created with Apps Script.\n  \u003c/body\u003e\n\u003c/html\u003e"
        }
      ]
    };

    var options = {
      "headers": {
         'Authorization': 'Bearer ' +  driveService.getAccessToken(),
       }, 
      "contentType": "application/vnd.google-apps.script+json",
      "method" : "post",
      "payload": JSON.stringify(requestBody)
    }

    var response = UrlFetchApp.fetch(url, options);
    return HtmlService.createHtmlOutput(response.getContentText());
  }
}

在选项变量中,尝试更改“body:”的有效负载参数,并忽略元数据。仍会创建未知类型的新驱动器文件,但不会插入文本或任何内容。我不认为
body
是中的高级参数,也不认为
payload
是等效的。
Utilities.newBlob()
是我遗漏的。因为某种原因,我认为blob必须来自
响应。getBlob()
。将在我的附加组件中实现。thx一吨!等等,您是手动插入unicode
\u003c
的转换,还是为此使用了库/映射?我想对Drive.Files.update(resource、fileId、blob)执行此操作,但我得到了“应用程序没有更新应用程序脚本所需的作用域”。使用高级驱动器服务不起作用,因为您无法为它设置自定义令牌,也无法让应用程序脚本为您请求它:(用于
上载
方法的可选查询参数
convert
现已弃用。它没有任何功能。
function createGoogleFileInFolder3() {  
    var requestBody =  {
      "files": [
        {
          "name":"Code",
          "type":"server_js",
          "source":"function doGet() {\n  return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
        },
        {
          "name":"index",
          "type":"html",
          "source":"\u003chtml\u003e\n  \u003cbody\u003e\n    Created with Apps Script.\n  \u003c/body\u003e\n\u003c/html\u003e"
        }
      ]
    };

  var resource = {
    "title": "Test script",
    "parents": [
      {
        "id": "<parent folder id here>"
      }
    ]
  };

  var blob = Utilities.newBlob(JSON.stringify(requestBody), "application/vnd.google-apps.script+json");

  Drive.Files.insert(resource, blob, {"convert":"true"});
}