phpgoogle-drive-apigoogle-oauthscopes

Google Drive API service->files->get('root') fails with "file not found.' (Scopes problem?)


I have a PHP script that fails (404, [file] not found) when the following code is executed:

$result = $service->changes->getStartPageToken() ;
$nextStartPageToken = $result->startPageToken ;
$rootResult = $service->files->get('root')

The the failure is at that last line, and the error log says:

[13-Aug-2023 20:24:47 America/New_York] PHP Fatal error:  Uncaught Google\Service\Exception: {
  "error": {
    "code": 404,
    "message": "File not found: 0AORSlah_iew_Uk9PVA.",
    "errors": [
      {
        "message": "File not found: 0AORSlah_iew_Uk9PVA.",
        "domain": "global",
        "reason": "notFound",
        "location": "fileId",
        "locationType": "parameter"
      }
    ]
  }
}
 in /www/cgi-bin/GoogleRelated/vendor/google/apiclient/src/Http/REST.php:134
Stack trace:
#0 /www/cgi-bin/GoogleRelated/vendor/google/apiclient/src/Http/REST.php(107): Google\Http\REST::decodeHttpResponse()
#1 [internal function]: Google\Http\REST::doExecute()
#2 /www/cgi-bin/GoogleRelated/vendor/google/apiclient/src/Task/Runner.php(187): call_user_func_array()
#3 /www/cgi-bin/GoogleRelated/vendor/google/apiclient/src/Http/REST.php(66): Google\Task\Runner->run()
#4 /www/cgi-bin/GoogleRelated/vendor/google/apiclient/src/Client.php(922): Google\Http\REST::execute()
#5 /www/cgi-bin/GoogleRelated/vendor/google/apiclient/src/Service/Resource.php(238): Google\Client->execute()
#6 /www/cgi-bin/GoogleRelated/vendor/google/apiclient-services/src/Drive/Resource/Files.php(205): Google\Service\Resource->call()
#7 /www/htdocs/gdrive/catchupSongBook(592): Google\Service\Drive\Resource\Files->get()
#8 /www/htdocs/gdrive/catchupSongBook(80): getRootFolderId()
#9 {main}
  thrown in /www/cgi-bin/GoogleRelated/vendor/google/apiclient/src/Http/REST.php on line 134

Using a different project JSON file, I have a working script that successfully executes the same exact code (and so much more).

Since it seems impossible to have a google drive that lacks "root" folder (hence, the error message sounds misleading), I figure the issue here is in the scopes used or in the project itself, but I cannot see any problem with my current selection or the project attributes. I've looked at answers like: this one, for java that pretty much says this is the right approach, and not found a better way to accomplish what I need here.

The scopes I'm requesting are:

"https://www.googleapis.com/auth/drive.appdata"
"https://www.googleapis.com/auth/drive.appfolder"
"https://www.googleapis.com/auth/drive.file"
"https://www.googleapis.com/auth/drive.resource"

Really, I have three questions.

First is whether the issue might be scopes-related

Second is whether the above list lacks anything I need

Third, is there a way to see what scopes were actually granted, rather than only what was requested?


Solution

  • Answer to 1st question

    First is whether the issue might be scopes-related

    From your showing script and scopes, I'm worried that your current issue might be due to First is whether the issue might be scopes-related. From $rootResult = $service->files->get('root') and the ID of 0AORSlah_iew_Uk9PVA, it seems that you are trying to retrieve the metadata of the root folder ID. I thought that in the case of your current scopes, the file metadata of the files created by the client can be retrieved by drive.file. But, in this case, I thought that the root folder cannot be retrieved, because the root folder is not created by the client.

    In this case, how about adding a scope of https://www.googleapis.com/auth/drive.metadata.readonly or https://www.googleapis.com/auth/drive.readonly? Or, how about replacing drive.file to drive.metadata.readonly or drive.readonly?

    Answer to 2nd question

    Second is whether the above list lacks anything I need

    In this case, I have no information on the actual goal of your script. So, I cannot answer whether the scopes of "drive.appdata", "drive.appfolder", "drive.resource" are required to be used. But. if you are not required to retrieve only the files except for the files created by your current client, I think that https://www.googleapis.com/auth/drive.file is not required to be used.

    If you want to retrieve only the file metadata of your Google Drive, I think that just https://www.googleapis.com/auth/drive.metadata.readonly might be able to be used. In this case, the metadata of the root folder can be also retrieved.

    Answer to 3rd question

    Third, is there a way to see what scopes were actually granted, rather than only what was requested?

    If you want to confirm the scopes of your access token, how about using the following endpoint?

    curl "https://www.googleapis.com/oauth2/v3/tokeninfo?access_token={your access token}"
    

    By this, you can see the current scopes of the access token.