3

I tried several things bu couldn't get the root folder id in Java Api v3.

What I tried:

String id = service.files().get("fileId=root").setFields("id").execute().getId();

String id = service.files().get("fileId=root").setFields("?fields=id").execute().getId();

String id = service.files().get("root").setFields("?fields=id").execute().getId();

String id = service.files().get("fileId=root and ?fields=id").execute().getId();

I am getting this errors:

    com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "location" : "fields",
    "locationType" : "parameter",
    "message" : "Invalid field selection ?fields=id",
    "reason" : "invalidParameter"
  } ],
  "message" : "Invalid field selection ?fields=id"
}

An error occurred: com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
  "code" : 404,
  "errors" : [ {
    "domain" : "global",
    "location" : "fileId",
    "locationType" : "parameter",
    "message" : "File not found: .",
    "reason" : "notFound"
  } ],
  "message" : "File not found: ."
}

I looked the migration document and other posts but still couldn't get the root id. Thanks!

Community
  • 1
  • 1
user2640782
  • 1,074
  • 8
  • 15
  • Just curious, why you require the root id? – codePG Jan 05 '17 at 11:27
  • I am doing a migration from API v2 to v3. The previous code uses this id in several calls so I am trying to get the id. – user2640782 Jan 05 '17 at 11:29
  • If this could help, In the Try API [here](https://developers.google.com/drive/v3/reference/files/get), I just did `files.get` for a file in the root and passed `parents` as field option and I got the `ID` – codePG Jan 05 '17 at 11:34
  • Thanks for the solution, I will try it. However, in the migration document it says "for rootFolderId do files.get with fileId=root and ?fields=id". Do you know how can I get this work? – user2640782 Jan 05 '17 at 11:38
  • Yes, just tried with what you suggested. file.get("root").setFields("id") may be a good try. – codePG Jan 05 '17 at 11:44
  • It is working now. It was the only combination that I haven't tried :) Thank you! – user2640782 Jan 05 '17 at 11:49
  • I will add that as an answer and please accept if I have helped you. – codePG Jan 05 '17 at 11:50

2 Answers2

7

Try the following combination to see if that works.

String id = service.files().get("root").setFields("id").execute().getId();
codePG
  • 1,754
  • 1
  • 12
  • 19
  • 1
    Actually, in most cases, no need to get the root id. You can use the alias root to refer to the root folder anywhere a file ID is provided – skyfree Jul 07 '18 at 07:10
  • 1
    @skyfree the reason you would need the actual root id is where you get the files metadata and you want to check if it's parent is the root. The 'parents' property will contain the actual root id and not the string 'root'. – Gichamba Apr 25 '22 at 12:51
1

To execute the above combination, you have to give your application full access to use Drive. OAuth 2.0 scope that needs to be given: https://www.googleapis.com/auth/drive or https://www.googleapis.com/auth/drive.readonly

sgcode
  • 11
  • 3