Jonio
Jonio

Reputation: 1293

Api rest google drive folder not visible after creation by nodejs

When creating a folder via google api rest I cannot see the newly created folder from the google drive console. The code works, it returns the json with all the data of the folders just created.

function createFolder(nameProduct,folderId){
    console.log("nome folder : "+nameProduct);
    console.log("id parent : "+folderId);
    var fileMetadata = {
        'name': nameProduct,
        'mimeType': 'application/vnd.google-apps.folder',
        parent : [folderId]
      };
      let objFolder = {
        auth: jwToken,
        resource: fileMetadata,
        fields: 'id'
      }
      return new Promise((resolve,reject)=>{

      drive.files.create(objFolder).then(function(response) {
        // Handle the results here (response.result has the parsed body).
        console.log("Response", response);
        resolve(response);
        },
        function(err) { 
          // handle error here.
          reject(err);
          console.error("Execute error", err);
         });
      });
    }


var jwToken = new google.auth.JWT(
  key.client_email,
  null,
  key.private_key, ["https://www.googleapis.com/auth/drive"],
  null
);
jwToken.authorize((authErr) => {
  if (authErr) {
    console.log("error : " + authErr);
    return;
  } else {
    console.log("Authorization accorded");
  }
});

The authentication works fine, I'm able to crete file. I have this problem only with folder. Why is not visible on google drive ? The method return correctly the id

Upvotes: 2

Views: 677

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116958

You appear to be using a service account to authenticate. A service accounts are dummy users they have their own google drive account, google calendar account and probably a few more.

When you uploaded that file its uploaded to the service accounts google drive account. There is no web view for a service account the only access you have to it is programmaticlly.

options: upload to your account

Create a directory on your google drive account share that directory with the service account and upload to that directory.

Option: share the service accounts directory with you

Have the service account create a directory and then grant yourself permission on the directory. You will then be able to see that directory in your account.

Note

When the service account uploads files it will be the owner of that file make sure that you grant yourself permissions on these files or you wont be able to access them. Yes you can have a file on your drive account that you dont have permission to access. 😉

Upvotes: 1

Related Questions