2

I have a script that uploads a csv file to shared drive and it works fine, but when I try to create a folder it doesn't work. Not sure where I am going wrong Code that I am using to create a folder

    function createFolder() {
      var parents = '1XNBrOtnXJU1MhYv0nrAwTDXRZ'
      var fileMetadata = {
        'name': 'sample_folder',
        'mimeType': 'application/vnd.google-apps.folder',
        'parents' : [parents]
      };

  drive.files.create({
    resource: fileMetadata,
    fields: 'id'
  }, function (err, file) {
    if (err) {
      // Handle error
      console.error(err);
    } else {
      console.log('Folder Id: ', file.id);
    }
  });
         
}`

enter image description here

1
  • Did you loaded the gapi library? Commented Aug 4, 2021 at 19:46

2 Answers 2

3

You can still use the fetch API in your JavaScript. Just change the URL

From:

https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&supportsAllDrives=true

To:

https://www.googleapis.com/drive/v3/files?supportsAllDrives=true

change the mimeType in your metadata

From:

"mimeType": "text/csv",

To:

"mimeType": "application/vnd.google-apps.folder",

remove this line of code:

var form = new FormData();
form.append('metadata', new Blob([JSON.stringify(metadata)], { type: 'application/json' }));
form.append('file', file);

and use the metadata directly as your request body.

Your code should look like this:

var parents = "18-7shshazxhkashjfk-xO";
var metadata = {
  "name": filename,
  "mimeType": "application/vnd.google-apps.folder",
  "parents": [parents], // Google Drive folder id
};

var accessToken = gapi.auth.getToken().access_token;
fetch("https://www.googleapis.com/drive/v3/files?supportsAllDrives=true", {
  method: 'POST',
  headers: new Headers({
    'Authorization': 'Bearer ' + accessToken,
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  }),
  body: JSON.stringify(metadata),
}).then((res) => {
  return res.json();
}).then(function(val) {
  console.log(val);
});

Reference:

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much. I Tried the code it gives me a parse error .
@SarijaJanardh - I added JSON.stringify to the response body metadata. Kindly try again and let me know if it works.
it works now .Thank you so much for your timely help .
0

You have to provide application/vnd.google-apps.folder MIME type and a title.

This link may help https://developers.google.com/drive/api/v3/folder#create_a_folder

3 Comments

Hi Ashish .Thanks for the input. I did try changing the MIME Type but it doesn't seem to work with the drive.files.create. Is there a way using the fetch API .
@SarijaJanardh - Could you edit your post above and include the code you used in drive.files.create?
@NikkoJ. . I have edited my post with the drive.files.create

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.