3

I am using a service account to connect to a shared drive in my personal Google account. The Google Drive API always returns an error saying the shared drive was not found. I tried both of these:

  • making the shared drive public for anyone with the link
  • adding permission for a specific user (the service account) using the service account's email address

The link for the shared drive is in this format https://drive.google.com/drive/folders/xyz and I assume the driveId is the last part of the link, xyz? Or is that the folder id? If so then how do I find the driveId?

// load the service account credentials
data, err := ioutil.ReadFile("service-account.json")
if err != nil {
    log.Fatal("failed to read json file")
}

// parse the credentials file
conf, err := google.JWTConfigFromJSON(data, drive.DriveReadonlyScope)
if err != nil {
    log.Fatal("failed to parse json file")
}

apiKeyBytes, err := ioutil.ReadFile("api-key.txt")
API_KEY := string(apiKeyBytes)
DRIVE_ID := "1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm"

// send the GET request with all the parameters
client := conf.Client(context.Background())
parameters := "?key=" + API_KEY
parameters += "&corpora=drive"
parameters += "&includeItemsFromAllDrives=true"
parameters += "&supportsAllDrives=true"
parameters += "&driveId=" + DRIVE_ID
response, err := client.Get("https://www.googleapis.com/drive/v3/files" + parameters)

// read and print the response
data_buffer := make([]byte, 2048)
_, err = response.Body.Read(data_buffer)
response.Body.Close()
fmt.Println(string(data_buffer))

Here is the output when this program is run:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Shared drive not found: 1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm",
    "locationType": "parameter",
    "location": "driveId"
   }
  ],
  "code": 404,
  "message": "Shared drive not found: 1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm"
 }
}

I also tried the "Try this API" tool at this link https://developers.google.com/drive/api/v3/reference/files/list which was using OAuth 2.0 tied to my personal Google account instead of the service account, and that failed too.

  • I think that your request works when those values in the query parameters are valid values. So from your error message and `I also tried the "Try this API" tool at this link https://developers.google.com/drive/api/v3/reference/files/list which was using OAuth 2.0 tied to my personal Google account instead of the service account, and that failed too.`, how about confirming the drive ID and the permissions of the shared drive again? – Tanaike Jan 18 '22 at 01:15
  • @Tanaike I just created a test shared drive at [https://drive.google.com/drive/folders/1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm](https://drive.google.com/drive/folders/1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm) which is publicly available. When I use the driveId as 1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm in my code I still get the error. – Justice Oarry Jan 18 '22 at 01:40
  • Thank you for replying. From your replying, I proposed a modification point as an answer. Could you please confirm it? If that was not useful, I apologize. – Tanaike Jan 18 '22 at 01:54

1 Answers1

7

When I saw your sample URL of https://drive.google.com/drive/folders/1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm, I thought that in this case, it's a publicly shared folder. I thought that this might be the reason for your issue. In this case, how about the following modification?

From:

parameters := "?key=" + API_KEY
parameters += "&corpora=drive"
parameters += "&includeItemsFromAllDrives=true"
parameters += "&supportsAllDrives=true"
parameters += "&driveId=" + DRIVE_ID

To:

parameters := "?key=" + API_KEY
parameters := "&q=%271dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm%27%20in%20parents"
  • The value of q is '1dpl28_lhR1myDL2Y2gYKLRX1gNRlWdFm' in parents.
  • When your API key is valid key, this modification can be used. If an error occurs, please test this with "Try this API". Ref

Note:

  • When the metadata of the folder of the shared Drive is retrieved, the value of driveId is included in the returned value. When I tested your folder ID, this value was not included in the metadata. So I thought that your folder might be the publicly shared folder of Google Drive which is not the shared Drive.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • 1
    Thanks, that worked! I guess I was confusing shared drive with shared folder. – Justice Oarry Jan 18 '22 at 02:32
  • @Justice Oarry Thank you for replying and testing it. I'm glad your issue was resolved. I could understand from your provided sample URL. Thank you, too. – Tanaike Jan 18 '22 at 02:39
  • Using the query of target driveId in parents is genius. I could not get a `list(driveId='xxxx', ...)` query to work, despite being able to move files, download files, etc from the python API. – Merlin Jan 24 '23 at 17:38
  • 1
    @Merlin About your question, I would like to support you. But, I have to apologize for my poor English skill. Unfortunately, I cannot understand your question. So, can you post it as a new question by including more information? By this, it will help users including me think of a solution. When you can cooperate to resolve your question, I'm glad. Can you cooperate to do it? – Tanaike Jan 24 '23 at 22:53