0

I have a NodeJS app to read files in my google drive, source code like below:

  var d = google.drive({ version: "v2", auth: oauth2Client });
  var drive = d.drives.get({ driveId: "my-drive" });

need to have driveId as one of parameters, how to get it? besides, it's not shared drive, just my personal drive, I used drive name (my-drive) which is obvious wrong, URL and error message as below:

url: 'https://www.googleapis.com/drive/v2/drives/my-drive',

GaxiosError: Shared drive not found: my-drive

Tanaike
  • 181,128
  • 11
  • 97
  • 165
bim2016
  • 105
  • 1
  • 10
  • I've never worked with the Google Drive API. However, API's usually have the ability to list the resources (drives in this case) in them that you have access to. Have you tried that? – mason Feb 02 '22 at 00:31

2 Answers2

3

I believe your goal is as follows.

  • You want to retrieve the folder ID of the root folder of your Google Drive using googleapis for Node.js.

In this case, I thought that the method of "Files: get" can be used. When this is reflected in your script, it becomes as follows.

Modified script:

var d = google.drive({ version: "v2", auth: oauth2Client });
var drive = await d.files.get({ fileId: "root" });
var id = drive.data.id;
console.log(id);

Note:

  • In the case of var drive = d.drives.get({ driveId: "my-drive" });, drive is Promise. Please be careful this.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • thanks Tanaike, works great! I can get root folder now, yet to figure out how to display in tree list – bim2016 Feb 02 '22 at 06:12
  • @bim2016 Thank you for replying. I'm glad your issue was resolved. About your new question of `yet to figure out how to display in tree list`, I would like to support you. But that is a new issue, and that is different from your question. So can you post it as a new question? Because when your initial question is changed by comment, other users who see your question are confused. By posting it as a new question, users including me can think of it. If you can cooperate to resolve your new issue, I'm glad. Can you cooperate to resolve your new question? – Tanaike Feb 02 '22 at 06:17
  • thanks Tanaike, figured out tree stuff, in fact, my next question is about app verification, see below link FYI: https://stackoverflow.com/questions/70951158/how-to-verify-app-which-is-currently-using-localhost – bim2016 Feb 02 '22 at 08:29
1

Checking that you are trying to call your "My Drive" id however there is no API call to retrieve this information as it's not a folder per se, instead you can as well call the parent folders that are saved in your personal Drive, you can use a List that will return the parent folder ID about the file ID you are trying to work on.

The Drive.get method you are referring to only works for shared drive only as noted in the documentation:

Gets a shared drive's metadata by ID.

In this case you won't be able to use this API call if you are not trying to retrieve data from a Shared Drive itself.

Gabriel Carballo
  • 1,278
  • 1
  • 3
  • 9
  • thanks Gabriel, right, you can only get shared drive id, after using Tanaike's method, i can get root folder, now have to figure out how to display in tree list. – bim2016 Feb 02 '22 at 06:15