Write to a Google Doc using Drive API -
i looking create , write content google drive doc.
i have create drive doc step down, aren't able post content.
code create file test doc
function createnewfile() { gapi.client.load('drive', 'v2', function() { var request = gapi.client.request({ 'path': '/drive/v2/files', 'method': 'post', 'body': { "title": "test.doc", "mimetype": "application/vnd.google-apps.document", "description": "some" } }); request.execute(function(resp) { console.log(resp); updatefile(resp.id, "test text"); }); }); }
code add contents newly created google doc
function updatefile(fileid, text, callback) { const boundary = '-------314159265358979323846'; const delimiter = "\r\n--" + boundary + "\r\n"; const close_delim = "\r\n--" + boundary + "--"; var contenttype = "text/html"; var metadata = { 'mimetype': contenttype, }; var multipartrequestbody = delimiter + 'content-type: application/json\r\n\r\n' + json.stringify(metadata) + delimiter + 'content-type: ' + contenttype + '\r\n' + '\r\n' + text + close_delim; if (!callback) { callback = function(file) { console.log("update complete ", file) }; } gapi.client.request({ 'path': '/upload/drive/v2/files', 'method': 'put', 'params': { 'fileid': fileid, 'uploadtype': 'multipart' }, 'headers': { 'content-type': 'multipart/mixed; boundary="' + boundary + '"' }, 'body': multipartrequestbody, callback: callback, }); }
here post , update should like. can use browser console check doing differently.
insert
request url:https://www.googleapis.com/drive/v2/files/ request method:post content-type:application/json;charset=utf-8 {title: "delmexxx-0", mimetype: "application/vnd.google-apps.document"}
upload
request url:https://www.googleapis.com/upload/drive/v2/files/ 18ecxade2zfleo? convert=true&uploadtype=media request method:put content-type: text/plain content become google doc
Comments
Post a Comment