Search code examples
gogoogle-drive-api

Google Drive API error - "message": "Shared drive not found: xyz"


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.


Solution

  • 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: