Google drive api folder list files - google-drive-api

Suddenly getting files in public folder on google drive doesnt seem to get any results back.
I havent changed any code, this is the url I am using:
https://www.googleapis.com/drive/v3/files?q='FOLDER_ID'+in+parents&pageSize=1000&key=API_KEY
My api key works. Has anything changed?
when I try in api explorer: https://developers.google.com/drive/api/v3/reference/files/list
I only input q parameter (my folder id), I get:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "Invalid Value",
"locationType": "parameter",
"location": "q"
}
],
"code": 400,
"message": "Invalid Value"
}
}
My folder id is correct.
edit: I have created a new folder and added some files inside and now its working, but its still does not work with old folders. Not sure why. If this was the case that files were maybe deleted from my drive, (but still show in folder for some reason?) then it might explain the results.

Related

Aquiring a file list from a shared sub folder using the Google API list

Before writing java code I am trying to understand how the GoogleAPI works.
I am trying to get a list of files from a subdirectory of a shared drive,
If I use the file id from the high level shared folder I can get a list of all the files including subfolders.
Every time I try to get ONLY the files in a subfolder I get
{
"domain": "global",
"reason": "notFound",
"message": "Shared drive not found: 1S_4HV0vi3zPr-gt2lRTCp5hAJFooElCn",
"locationType": "parameter",
"location": "driveId"
}
I am using the website https://developers.google.com/drive/api/v3/reference/files/list
with the following parameters
Corpra = drive
deriveid
includeItemsFromAllDrives=true
pagesize=250
SupportsAllDrive=true
This is the partial output of the high level shared drive that I get the subfolder ID from
{
"kind": "drive#file",
"id": "1S_4HV0vi3zPr-gt2lRTCp5hAJFooElCn",
"name": "2021",
"mimeType": "application/vnd.google-apps.folder",
"teamDriveId": "0ACNyBdDB5wLXUk9PVA",
"driveId": "0ACNyBdDB5wLXUk9PVA"
},
What am I doing wrong ???
You cannot use the shared drive sub-directory folder id in Files.list().
This method requires a driveId which is the ID of the shared drive to search.
You can use Drives.list() to get a list of valid user shared drive
If you want to list the files inside the sub-directory folder, you need to add a query parameter that will indicate the file's parent folder id. See Search for files and folders
Note:
Some File Resource fields are not shown in the response body by default, if you want to show all fields, set fields parameter in Files.list() to *
For example:
My shared drive sub-folder id: '1V2bi9xN8aWDWzCvMWC4oCqmOGHh1xxxx'
If I want to list all files under this sub-folder, I need to include this query parameter: '1V2bi9xN8aWDWzCvMWC4oCqmOGHh1xxxx' in parents
Your request parameters will be:
Corpra = drive
deriveid
includeItemsFromAllDrives=true
pagesize=250
SupportsAllDrive=true
q = 'shared drive sub-folder id' in parents
Sample Response:
{
"kind": "drive#fileList",
"incompleteSearch": false,
"files": [
{
"kind": "drive#file",
"id": "1_XlY8tgcJgj13FItftGrF77ixxxxx",
"name": "subfolder_doc",
"mimeType": "application/vnd.google-apps.document",
"teamDriveId": "0AJWF4SKro9kyxxxx",
"driveId": "0AJWF4SKro9kyUxxxx"
}
]
}
(Update)
Here is a sample request in java using Drive.Files.List() methods
FileList result = service.files().list()
.setCorpora('drive')
.setDriveId('0ACNyBdDB5wLXUk9xxxx')
.setPageSize(250)
.setIncludeItemsFromAllDrives(true)
.setSupportsAllDrives(true)
.setQ("'1-mZjjMTrcmlNm6SnJQeSQxGxxxxx' in parents")
.execute();

Agument list files API call with createdTime attribute in Google Drive API

I'd like to retrieve all the resources (files and folder) via the Files: list
API call with the createdTime attribute included.
Standard response doesn't include the createdTime key described in the resource representation so I've added the fields query parameter with the value createdTime (there's an open issue related which says so but referring to API v2 while I'm using API v3).
API call I'm doing is the following:
https://www.googleapis.com/drive/v3/files?fields=createdTime
with proper authentication.
A 400 status code response is returned with the following body:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidParameter",
"message": "Invalid field selection createdTime",
"locationType": "parameter",
"location": "fields"
}
],
"code": 400,
"message": "Invalid field selection createdTime"
}
}
What's the correct way of using the fields query parameter?
If you want to see the full response you can do the following test here
https://www.googleapis.com/drive/v3/files?fields=*
If you just want the create date you can also do test here
https://www.googleapis.com/drive/v3/files?fields=files(createdTime)
You can read more about this in Partial response

Why are attempts to view revisions with the Google Drive API returning a 500?

I've been working with the Google Drive API for a bit, accessing the list of revisions for two sheets as part of a job with service.revisions().list(fileId=FILE_ID).execute(). Everything was fine until earlier this morning, when one of the requests (but not the other) started returning a 500 error. This isn't intermittent - it's returning this error on every request so an exponential backoff just hits the cap I've set (5).
There's nothing of value in the response but here it is:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "internalError",
"message": "Internal Error"
}
],
"code": 500,
"message": "Internal Error"
}
}
I get the same error when querying with the API explorer, so this seems to be an issue with the sheet itself as opposed to a coding problem. Here's the request URL:
https://www.googleapis.com/drive/v3/files/1KYDm4gqaCLipQTskbqQbesq8ON7CmV_4IscF8V0FZrs/revisions?key=YOUR_API_KEY
What could suddenly cause this change in behavior?

File not found error Google Drive API

I am using the Drive REST API to download a file. I am making a GET request using the file id and I get a file not found exception.
{
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "File not found: xxxxx",
"locationType": "other",
"location": "file"
}
],
"code": 404,
"message": "File not found: xxxxx"
}
}
I have also generated the apikey and I am using it in my GET request.The file does exist so I am unsure what I am missing here.
Make sure scopes are corrects
var url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: ['https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.metadata'
]
});
Drive API declares the following scopes. Select which ones you want to grant to APIs Explorer.
https://www.googleapis.com/auth/drive
View and manage the files in your Google Drive
https://www.googleapis.com/auth/drive.appdata
View and manage its own configuration data in your Google Drive
https://www.googleapis.com/auth/drive.file
View and manage Google Drive files and folders that you have opened or created with this app
https://www.googleapis.com/auth/drive.metadata
View and manage metadata of files in your Google Drive
https://www.googleapis.com/auth/drive.metadata.readonly
View metadata for files in your Google Drive
https://www.googleapis.com/auth/drive.photos.readonly
View the photos, videos and albums in your Google Photos
https://www.googleapis.com/auth/drive.readonly
View the files in your Google Drive
https://www.googleapis.com/auth/drive.scripts
Modify your Google Apps Script scripts' behaviour
Sorce: https://developers.google.com/apis-explorer/#p/drive/v3/drive.files.get
This is resolved. I wasn't providing the correct access_token when making the GET request for file metadata. I regenerated the authorization code, access_token and my code is working now.
In my case, I simply had not given access to the folder to my service account. Simply sharing it via the web interface solved the problem.
What email address to use:
Check the email address of your service account here:
https://console.cloud.google.com/iam-admin/serviceaccounts?project=NAME_OF_PROJECT&supportedpurview=project
The email address will look like this:
name-of-service-account#name-of-project.iam.gserviceaccount.com
I would check to see if the file you are trying to retrieve the metadata for is part of a team drive; if it is then you have to set supportsTeamDrives=True in the request.
If you are getting a response like this:
<HttpError 404 when requesting https://www.googleapis.com/drive/v3/files/REDACTED_FILE_ID/copy?alt=json returned "File not found: REDACTED_FILE_ID.". Details: "[{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: REDACTED_FILE_ID.', 'locationType': 'parameter', 'location': 'fileId'}]">
And the fileId points to a file on a Shared Drive, you'll need to include supportsSharedDrives=true in your request parameters.
Google provides more detail on this in their Implement shared drive support article.
Here's a small example with Python for creating a copy:
googledrive = build('drive', 'v3', credentials=creds)
copy_response = googledrive.files().copy(
fileId=TEMPLATE_SPREADSHEET_ID,
body={
"parents": [
report_folder_id,
],
'title': report_title
},
supportsAllDrives=True
).execute()
In order to run that, you'll want to mix it into the example code from the Python Quickstart for Google Drive API.
I got to this question because I was getting a "File not found" error when using the update method.
I was using this line in Python to execute the method:
files_res.update(fileId=f_id, body={"name": f_name}).execute()
I could list the shared file and its parent fine, but update failed, no matter what the scope with this error:
...
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "File not found: 1YqV...",
"locationType": "parameter",
"location": "fileId"
}
],
"code": 404,
...
I added the supportsAllDrives=True param to the method and it worked:
files_res.update(fileId=f_id, body={"name": f_name}, supportsAllDrives=True).execute()
In my case, I was using the file client_secret.json that I had created with the gmail address I use all the time, but I was creating the credentials with another e-mail.

Google Drive API - list files in folder

As may devs, I want to get a list of all files in a specific folder.
I have the following basic tree structure
root
swimmer
trainings
test1.doc
test2.doc
logs
resultsA.csv
resultsB.csv
trying to list files in root/swimmer/trainings , I tried to use :
GET https://www.googleapis.com/drive/v2/files/trainings/children
and tested it within https://developers.google.com/drive/v2/reference/children/list
but I get the 404 response :
404 Not Found
- Show headers -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "File not found: trainings"
}
],
"code": 404,
"message": "File not found: trainings"
}
}
I also tried : GET https://www.googleapis.com/drive/v2/files?maxResults=10&projection=BASIC&q='trainings'+in+parents&key={YOUR_API_KEY}
same result not found ..
where am I wrong ?
Many things wrong in both your http urls. Look up the docs carefully.
In #1 its not the folder name, its the folder id (or else how do you think it would know which of the possibly several folders named 'trainings' to use?)