EDIT: I'm a dummy. The documentation has it spelled out plain as day here: https://developers.google.com/drive/v3/web/manage-uploads
Project overview for context: I'm using node.js to write a twitter bot deployed on Heroku. I'd like this bot to be able to donwload a .txt file (or an .ini or whatever) from Google Drive, use that file to compose a tweet, edit the body of the text file to reflect the tweet that was generated, then overwrite the original file in Google Drive. It will always be downloading, editing, and overwriting the same file, so no file selection dialogue is necessary.
Specific issue: I can't sort out how to actually upload the body of the text file without using an html page. This is all supposed to happen server side with no user interaction. Most of the examples I've found seem to use html pages to gather up and send the file information, but I'm using purely javascript, so that doesn't work for my project. I've searched the Google Drive API documentation for examples, but so far no luck.
The code so far:
The comment block midway through is where I'm having trouble.
authClientUp.authorize(function(err, tokens) {
if (err) {
console.log(err);
return;
} else {
console.log('It authorized successfully!');
drive.files.insert({ // ---- For the purposes of getting
auth: authClientUp, // ---- started, I'm just creating
"title": "file1.txt", // ---- a new file instead of overwriting
"mimeType": "text/plain", // ---- an existing one
"description": "Just a test file"
}, function(err,result){
if (err) {
console.log(err);
} else {
console.log(result);
drive.files.put({ //----- files.put() is not correct, but
//----- I'm unsure what the correct function is
/*
I know I need the following info here but I'm
not sure how to pass it in correctly:
- the id of the file to overwrite (taken from the JSON file 'result'
- the body of the file to upload
- authentication? I'm not sure if I need that agian
*/
}, function(err, result){
if (err) {
console.log(err);
} else {
console.log(result);
}
});
}
});
}
});
All the examples I've found format this step something like:
PUT /upload/drive/v2/files/{id}?uploadType=media HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer <OAuth 2.0 access token here>
Content-Type: mime/type
<file content here>
But outside the context of an html file I don't think that works? I need help to format this correctly for my project.
Notes:
- The purpose of writing this file out to Google Drive is so that I can commit changes to my Heroku repo without the contents of the .txt file being overwritten and lost
- I'm not an experienced programmer, and I'm new to javascript.
I've searched through numerous other questions here on Stack Overflow, but they all seem to hinge on one or more of the following things:
- An html form
- python, not javascript
- Google Drive Android SDK, which I'm not using (though if it comes to it, I guess I could)