【发布时间】:2014-11-24 00:44:57
【问题描述】:
尝试通过调用 Apps 脚本中的 Drive 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"
}
]
}
来自import/export docs 和example video Dan 提到调用是针对 Apps 脚本之外的语言,但在使用 Eric 设置授权后请求脚本文件类型列表和这些文件的内容does work oAuth2 库。
我最近的猜测...
function createProject( ) {
var token = getDriveService().getAccessToken(); // from Eric's oAuth2 lib
var url = 'https://www.googleapis.com/upload/drive/v2/files?convert=true';
// Where does this go?
var files = {
"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 Hello, world!!\n \u003c/body\u003e\n\u003c/html\u003e"
}
]
};
// Where does this go too?
var metadata = {
'title': 'script-test1',
'mimeType': 'application/vnd.google-apps.script',
"parents": [
{
"id": "0B2VkNbQMTnaCODBRVjZQcXBXckU"
}
],
};
var options = {
'headers' : {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/vnd.google-apps.script+json',
},
'method' : 'POST',
'payload' : files // probably not right
};
var response = UrlFetchApp.fetch(url, options);
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);
...引发内部错误。
还知道Drive insert docs 有一个客户端 JS 示例,但不知道应该将其中多少内容(如果可能)转换为服务器端 Apps 脚本。
【问题讨论】:
-
在您的选项变量中,尝试更改“body:”的有效负载参数,然后忘记元数据。
-
仍会创建一个未知类型的新云端硬盘文件,但未插入任何文本或任何内容。我没有将
body视为UrlFetchApp docs 中的高级参数,并将payload解释为等效参数。
标签: api google-apps-script google-drive-api