401-Unauthrized when downloading file for Google Team drive - google-drive-api

I am trying to access Team drive files using Drive Picker.
But I am getting this error
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
} ],
"message" : "Invalid Credentials"
}
So I tried to check in Google API Explorer and called the API by passing fileId and supportsTeamDrives = true. But in Google API explorer also, its giving me the same error.
What I am missing here?

Accessing Teamdrives uses a different method. List the files of the particular Teamdrive using the method Teamdrives: list, from there you will be able to get the Teamdrive id from the response just like below:
{
"kind": "drive#teamDrive",
"id": "XXXXXXIDXXXXXXX",
"name": "TheFileName"
}
You can then use the specific file id to get the metadata of a specific file from the method Teamdrives: get.

Related

Listing or getting permissions assigned to the folders under Shared Drive

I am trying to list all permissions assigned to files/folders under the shared drive for the purpose of retrieving external collaborators.
It seems I have proper access right for All the files and folders since I can list/get them via drive API, but when it comes the the permission, the API claims the file/folders do not exist.
Is this the known behavior, or there is another way of retrieving permissions from files/folders in the shared drive?
request and response
request
GET https://www.googleapis.com/drive/v3/files/[FILE ID]/permissions?useDomainAdminAccess=true&key=[YOUR_API_KEY] HTTP/1.1
response
{
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "File not found: [FILE ID].",
"locationType": "parameter",
"location": "fileId"
}
],
"code": 404,
"message": "File not found: [FILE ID]."
}
}
Answer:
You need to make your request with useDomainAdminAccess set to false and supportsAllDrives set to true.
Example Request:
GET https://www.googleapis.com/drive/v3/files/[FILE_ID]/permissions?supportsAllDrives=true&key=[YOUR_API_KEY]

Google Drive API v3: how to set canCopy capabilities on a file

I want to limit the capabilities of a file in personal Google Drive. When I use the API explorer Google Drive v3 API with the following request:
POST https://www.googleapis.com/drive/v3/files?key={YOUR_API_KEY}
{
"capabilities": {
"canCopy": false
},
"name": "testfile"
}
I get the following response:
403
- Show headers -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "fieldNotWritable",
"message": "The resource body includes fields which are not directly writable."
}
],
"code": 403,
"message": "The resource body includes fields which are not directly writable."
}
}
Which is the same response that I get when trying to update a file with:
PATCH https://www.googleapis.com/drive/v3/files/FileIdHere?key={YOUR_API_KEY}
{
"capabilities": {
"canCopy": false
}
}
It seems that there are no methods to accomplish this or has anyone a workaround?
If you check file resource you will notice that some fields are writeable while others are not.
The message you are seeing is because the field you are trying to set is not writeable this means you can not change it from the API

Not able to download file from Google Drive with Postman

I want to use Google Drive from Postman, and when I tried to hit the API I am geting an error.
URL:
https://www.googleapis.com/drive/v3/files/1Iwvqcx073GhEU9eRD4vPihXoj2AV63LEyLZwu6i0jTQ?alt=media
I am getting:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "fileNotDownloadable",
"message": "Only files with binary content can be downloaded. Use Export with Google Docs files.",
"locationType": "parameter",
"location": "alt"
}enter code here
],
"code": 403,
"message": "Only files with binary content can be downloaded. Use Export with Google Docs files."
}
}
you need to add responseType: 'blob' if you are trying to get a pdf file
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data
Check this link for more info on how to handle file content download

Google drive api search query with partial match on property value

I am trying to use Google drive api through files().list().setQ(q).execute() where I have
q = "properties has { key='Code' and value contains 'abc' and visibility='PUBLIC' }"
Is there no option to use "contains" operator for value? I want to do partial matching on the value.
I get the following error (used in java).
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request { "code" : 400, "errors" : [ { "domain" : "global", "location" : "q", "locationType" : "parameter", "message" : "Invalid Value", "reason" : "invalid" } ], "message" : "Invalid Value" }
The exact same query works if I replace "contains" with "=" when there is an exact match.

How do i find a use the drive SDK to find a file using only drive.file scope

My scenario is that I want to look for a file, that if it exists, was created by my app.
If I request drive scope, I can use
/drive/v2/files?q=trashed%3Dfalse+and+title+%3D+'MyFile'+and+'root'+in+parents
and all is good.
But this requires access to all the users existing drive files, which is too demanding.
So I removed the drive scope and try the same command with only drive.file scope.
In return I get
{
"code" : 500,
"errors" : [ {
"domain" : "global",
"message" : "Internal Error",
"reason" : "internalError"
} ],
"message" : "Internal Error"
}
Hacking around, if I remove the 'root' in parents, I get
{
"kind": "drive#fileList",
"etag": "\"Q5ElJByAJoL0etObruYVPRipH1k/vyGp6PvFo4RvsFtPoIWeCReyIC8\"",
"selfLink": "https://www.googleapis.com/drive/v2/files?q=trashed%3Dfalse+and+title+%3D+'MyFile'",
"items": []
}
which is better than a 500, but still not what I expected.
So how can I check if MyFile exists without requiring drive scope?
This is a known bug we're fixing soon.
GET https://www.googleapis.com/drive/v2/files/children?q=title+contains+'My title'
will also only list files from root that your application has right to access.