Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
Create folders in a shared drive ( Google Drive API) with Javascript

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

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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:

  • Google Drive API Files:create
about 4 years ago · Juan Pablo Isaza Denunciar

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda