2

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)
2
  • Given your use case is "downloading, editing, and overwriting", you might find that the spreadsheet API is a better fit. It allows you to only upload the changed cells, leaving the rest of the file intact. Commented Mar 18, 2017 at 9:13
  • Thank you for the suggestion! I'll look into that, as it sounds like it suits my purposes perfectly. Commented Mar 18, 2017 at 15:15

1 Answer 1

5

Check this Google Drive REST API documentation:

https://developers.google.com/drive/v3/web/manage-uploads

The example there, literally:

var fileMetadata = {
  'name': 'photo.jpg'
};
var media = {
  mimeType: 'image/jpeg',
  body: fs.createReadStream('files/photo.jpg')
};
drive.files.create({
   resource: fileMetadata,
   media: media,
   fields: 'id'
}, function(err, file) {
  if(err) {
    // Handle error
    console.log(err);
  } else {
    console.log('File Id: ', file.id);
  }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Aw man, now I feel like a dumbass. Thank you! I think I did encounter this earlier on in the project, but I didn't have authentication figured out yet so it didn't work, so I dismissed it. Sorry for wasting everybody's time!
It happens to all of us to sometimes not see the solution in front of us, so no worries
just need to put the auth object drive.files.create({ auth: auth, resource: fileMetadata, media: media, fields: 'id' }, function (err, file) { if (err) { // Handle error console.log("error occurred while uploading to gdrive" + err); } else { console.log('gdrive File created with Id: ', file.id); } });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.