Google drive get files with API-KEY - google-drive-api

I use below url for get files
https://www.googleapis.com/drive/v2/files?name=xxx&key=yyy
But I get this error:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "Insufficient permissions for this file",
"locationType": "other",
"location": "file.permissions"
}
],
"code": 403,
"message": "Insufficient permissions for this file"
}
}
I want get lists only with key
How I can?

I believe your goal as follows.
You want to retrieve the file list of Google Drive using your API key.
You want to achieve this using Drive API v2.
Issue and workaround:
The API key can be used for the public contents. So, unfortunately, you cannot all files on your Google Drive from the root folder using the API key. Because the root folder of Google Drive cannot be publicly shared. I thought that your error message might be due to this. (I'm not sure what you want to do using name=xxx. But, https://www.googleapis.com/drive/v2/files?key=yyy is trying to retrieve the file list of all files of Google Drive. In this case, such error occurs.) But, fortunately, when a folder in your Google Drive is publicly shared, the file list can be retrieved using the API key.
In this answer, I would like to introduce the method for retrieving the file list from the publicly shared folder in the Google Drive.
Sample endpoint:
When you test the following endpoint, please publicly share a folder in your Google Drive and use the folder ID of the folder. By this, the file list of the folder can be retrieved using the API key.
https://www.googleapis.com/drive/v2/files?q=%27folderId%27%20in%20parents&key=[YOUR_API_KEY]
In this endpoint, 'folderId' in parents is used as the search query. So, in this case, the folder of folderId is required to be publicly shared. When you use this script, please replace folderId to the folder ID of your folder which is publicly shared.
When Drive API v3 is used, you can use the following endpoint.
https://www.googleapis.com/drive/v3/files?q=%27folderId%27%20in%20parents&key=[YOUR_API_KEY]
Note:
When you want to retrieve the file list including the subfolders with the folder structures, it is required to traverse the folders. In this case, I think that it is required to use the script.
This answer supposes that your API key can be used Drive API. Please be careful this.
References:
Files: list of Drive API v2
Files: list of Drive API v3

Related

Get file permissions list in Google Shared Drive with Apps Script

I'm trying to get folder and file permissions which is located in Shared Drive. All I can get is permissions list for root Shared Drive but trying to run below code on any ID of file or folder inside throw error.
Relavant Facts
We are using Google Workspace Business Plus subscription
I am domain superadmin
I have access to this file in Shared Drive but I expect to have access to every file as an domain admin.
Right now I'm calling the below script from web IDE of Apps Script so it is working in my grant scope.
I am the script owner
Script is on My rive
Script
function getPermissions_(driveFileID, sharedDriveSupport){
var thisDrivePermissions = Drive.Permissions.list(
driveFileID,
{
useDomainAdminAccess: USE_DOMAIN_ADMIN_ACCESS,
supportsAllDrives: sharedDriveSupport
}
);
return thisDrivePermissions;
}
Errors
GoogleJsonResponseException: API call to drive.permissions.list failed with error: Shared drive not found: xxxxxxxx
GoogleJsonResponseException: API call to drive.permissions.list failed with error: File not found:
How can I get the file permissions list in Google Shared Drive with Apps Script?
As superadminin you don't have access to all files by default through Google Drive user interface / Google Apps Script. In order to get access to all files in your domain throught Apps Script you have to make use of domain-wide deletegation of authority.
The "not found" errors might occur because the file or shared drive ids are wrong or you don't have access, do double check that the ids are correct and that you passing the parameters correctly. For this you could use Logger.log
/ console.log to print the variable values to the execution log or use the Apps Script debugger.
Related
How to add a Google Drive folder to "My Drive" section to other users
Take ownership of other users Drive-files using Google Apps Script
If I understand correctly, you need to list all the permissions that a drive file contained in a folder from a shared drive has. To do that, you may check the following script as an example:
function myFunction() {
var driveFileID = "FILE ID";
var sharedDriveSupport = true;
var request = {
"supportsAllDrives": sharedDriveSupport,
"useDomainAdminAccess": true,
"fields": "permissionIds"
};
var permissionsRequest = {
"supportsAllDrives": sharedDriveSupport,
"fields": "role"
};
var permissionsIDs = Drive.Files.get(driveFileID, request);
for(var i=0; i<permissionsIDs.permissionIds.length; i++)
{
Logger.log((Drive.Permissions.get(driveFileID, permissionsIDs.permissionIds[i], permissionsRequest)).role);
}
}
This is how it would display the information, by listing all the permission types from the specified file:
Note that this works because you mentioned you have access to the shared drive, however as Rubén mentioned in his answer Google Workspace admins do not have access to all the files, just to the ones that are shared with them, and if you want to have access you can do it using domain wide delegation and user impersonation.
References:
Files: get
Permissions: get

Drive.Files.get(fileid).LastModifiyingUser.emailaddress can't find the file in the shared drive using Google Apps Script

For a script that documents all the spreadsheet files with references in formulas to another spreadsheet, I want to retrieve the last modifying user of a file.
This apps script is only being used within the domain I am owning; the spreadsheet documents exist both in MyDrive as in our shared drive.
I am using the following statement:
var docModifierEmail = Drive.Files.get(docId).lastModifyingUser.emailAddress
Documentation is very hard to find
The error I'm getting all the time is:
API call to drive.files.get failed with error: File not found: 1Nz0_Kme172EQXAwgW55d7H.....
The scope I am using:
> "oauthScopes": [
> "https://www.googleapis.com/auth/spreadsheets",
> "https://www.googleapis.com/auth/userinfo.email",
> "https://www.googleapis.com/auth/drive",
> "https://www.googleapis.com/auth/drive.activity" <--- not sure if this is needed
Questions that I have:
Am I using the right function call?
Am I using the right scope?
What should be the right statement to retrieve the last modifying user's
email address of the file with id docId?
The Drive API version 2 available in Apps Script needs at least one of the following scopes for Files.get():
Scope:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.readonly
https://www.googleapis.com/auth/drive.metadata.readonly
https://www.googleapis.com/auth/drive.appdata
https://www.googleapis.com/auth/drive.metadata
https://www.googleapis.com/auth/drive.photos.readonly
When trying to access a file from your shared drive/team drive, You need to set the optional query parameter supportsAllDrives to true
Drive.Files.get("file id",{supportsAllDrives:true}).lastModifyingUser.emailAddress;
Additional Reference:
File Resource Representation

How Google drive third party shortcuts work

I was reading about third party shortcuts in Google Drive and able to create a file in Google Drive with the sample payload mentioned in the link.
var fileMetadata = new File()
{
Name = "Project plan",
MimeType = "application/vnd.google-apps.drive-sdk"
};
var request = driveService.Files.Create(fileMetadata);
request.Fields = "id";
var file = request.Execute();
Console.WriteLine("File ID: " + file.Id);
What I am not able to get is where can I set the external file URL here? Or how does it even work?
Answer
It is up to the third-party to handle the redirect to the relevant file for which a shortcut points to. This is done with the Google Drive File ID.
More Information:
Google Drive shortcuts are used to link a file, the contents of which are stored elsewhere than on Drive, such as a third party web app. This isn't done through links, as Drive shortcuts contain no content and are made using the file metadata endpoint.
When you make a Third-party shortcut using the Files.Create method as you show above, the indicative information of the fact it is a shortcut is by the application/vnd.google-apps.drive-sdk mimeType, using the POST URL for inserting file metadata:
POST https://www.googleapis.com/drive/v3/files
Authorization: <AUTHORIZATION HEADER>
{
"title": "File's title",
"description": "File's description",
"mimeType": "application/vnd.google-apps.drive-sdk"
}
When the shortcut is clicked, you will then get redirected to the third party web site from which the file was created - the Google Drive File ID is contained inside the ?state query parameter, as per the documentation on Open Files.
The important thing to know is that it is completely up to the third-party app/website to use Google Drive File ID in the ?state parameter to match the file and content they have stored.
References:
Create a shortcut to an external file
Handle an Open URL
Files: create | Google Drive API | Google Developers
Configure a Drive UI Integration

This is the code written according to the official documentation, I do not know which side went wrong [duplicate]

I want to retrieve list of files from a Google drive folder. Authentication happens through Service account. Here is my code to do the same:
final _credentials = new ServiceAccountCredentials.fromJson(r'''
{
"private_key_id": "b5-xxxx-17",
"private_key": "-----BEGIN PRIVATE KEY-----\nMI-xxxxk=\n-----END PRIVATE KEY-----\n",
"client_email": "drive-access#xxxx.iam.gserviceaccount.com",
"client_id": "100000000000",
"type": "service_account"
}
''');
final _SCOPES = [SheetsApi.DriveFileScope, SheetsApi.SpreadsheetsScope];
clientViaServiceAccount(_credentials, _SCOPES).then((http_client) {
DriveApi driveApi = DriveApi(http_client);
driveApi.files.list().then((files) {
print('kind: ' + files.kind);
print('list: ' + files.files.length.toString());
});
My log looks like this:
just: drive#fileList
list: 0
In Google Developers console, Google Drive API is enabled and service account it linked properly (as far as I can check).
But I also got another piece of code which writes some data to a spreadsheet, with hardcoded sheetID and that code is working fine.
Any help on what I am doing wrong here?
Retrieving files using Service account:
The service account is different from your Google account. This means that the Google Drive is also different between Service account and your account. So when the file is retrieved using Service account, please share the files in your Google Drive with the Service account. By this, the files in your Google Drive can be retrieved by the Service account.
Scopes:
In your script, DriveFileScope and SpreadsheetsScope are used as the scopes. DriveFileScope is https://www.googleapis.com/auth/drive.file. The official document says this scope as follows.
View and manage Google Drive files and folders that you have opened or created with this app
By this, in your script, how about modifying DriveFileScope as follows?
DriveReadonlyScope (https://www.googleapis.com/auth/drive.readonly)
DriveScope (https://www.googleapis.com/auth/drive)
Note:
I think that this scope can be also used for your situation.
DriveApi.driveMetadataReadOnlyScope (https://www.googleapis.com/auth/drive.metadata.readonly)
References:
Scopes for Google Sheets API, v4
Scopes for Drive API, v3

Rename Folder in Google Drive via API

Is there a way to rename a Google Drive folder via the REST API?
The folders I want to do this with typically have subfolders and files inside them.
I've found documentation for renaming files, but nothing about folders.
Use Drive API files.update. Your request body should look something like:
{
"name": "newFileName"
}
Folders are treated as files in the v3 API, therefore the endpoint you mentioned will work for folders, as well as for files.
If you want to do this via the REST API, you can do so by making a PATCH HTTP request to
https://www.googleapis.com/drive/v3/files/[fileOrFolderId]
The request body should include:
{
"name": "newFolderName"
}