Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive API v3 getting root folder Id in java

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!

like image 931
user2640782 Avatar asked Sep 24 '26 12:09

user2640782


2 Answers

Try the following combination to see if that works.

String id = service.files().get("root").setFields("id").execute().getId();
like image 119
codePG Avatar answered Sep 26 '26 02:09

codePG


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

like image 40
sgcode Avatar answered Sep 26 '26 01:09

sgcode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!