Trying to create a new project with files with a call to the Drive SDK within Apps Script.
Where exactly would the following go in a UrlFetchApp
request...
{
"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"
}
]
}
It's from the import/export docs and in the example video Dan mentions the calls are for languages outside of Apps Script but requesting a list of script file types and the content of those files does work once the authorization is setup with Eric's oAuth2 library.
My most recent guess...
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());
}
An untitled Drive file of unknown type does get created and the payload does get inserted into it, but it's not converted to a script file type.
Going another route and just using...
var file = {
"title": "Test script",
"mimeType": "application/vnd.google-apps.script",
"parents": [
{
"id": "[INSERT FOLDER ID HERE]"
}
]
};
Drive.Files.insert(file);
...throws an Internal error.
Also aware of the Drive insert docs that have a client-side JS example, but don't know how much of it should be translated over (if possible) to server-side Apps Script.