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.
Related
We've developed a Google Marketplace app using AppsScript which we've now made live on the marketplace (https://workspace.google.com/marketplace/app/report_connector_for_salesforce_beta/977246938879). However, we're struggling to work out how to use the Google Marketplace API (https://developers.google.com/workspace/marketplace/example-calls-marketplace-api) to access the license data. Has anyone else successfully managed this?
Has anyone else managed to get this working? It would be great if there was an app for this by the way on the marketplace!
Many thanks
Andy
So far, we've created a separate Google Sheet (in the same domain as our app), and we've written a simple API call function as follows:
function getUsers() {
const options = {
muteHttpExceptions: true,
headers: {
Authorization: `Bearer ${ScriptApp.getOAuthToken()}`
}
};
let response = UrlFetchApp.fetch("https://appsmarket.googleapis.com/appsmarket/v2/licenseNotification/<our app Id>",options);
Logger.log("Refresh Response:"+response);
}
We've created a new project in the GCP, and have enabled:
Google Workspace Marketplace API
Google Workspace Marketplace SDK
We've added the following scopes to the project:
Connect to an external service https://www.googleapis.com/auth/script.external_request
View your installed application's licensing information https://www.googleapis.com/auth/appsmarketplace.license
But, when we run the code, we just keep getting the following:
Info Refresh Response:{
"error": {
"code": 403,
"message": "Not authorized to access the application ID",
"errors": [
{
"message": "Not authorized to access the application ID",
"domain": "global",
"reason": "forbidden"
}
]
}
}
This worked for me, but I needed to add "https://*.googleapis.com/" in the urlFetchWhitelist.
Is your App Id correct? Check it out at https://console.cloud.google.com/apis/api/appsmarket-component.googleapis.com/googleapps_sdk?project=*projectName*
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.
I'm able to give writer permissions using the Google Drive API (v3). This is the endpoint:
POST https://www.googleapis.com/drive/v3/files/FILE_ID/permissions"
The API reference says that I should use sendNotificationEmail: false (note, this is a boolean not a string), which is what I'm doing (in v2 this was called sendNotificationEmails).
However, after the call I still get the email from Google saying I've been invited to edit a file. 🤔
sendNotificationEmail: false works as intended, there is one small detail:
It is a request parameter that goes outside of the resource body.
I am not sure which language your are using, if e.g. Javascript, the request would be:
gapi.client.drive.permissions.create({
"fileId": "XXX",
"sendNotificationEmail": false,
"resource": {
"role": "writer",
"type": "user",
"emailAddress": "test#gmail.com"
}
})
I am using this code in Apps script
function getUserObjByEmail(email){
// Same as using AdminDirectory class.
var apiUrl = "https://www.googleapis.com/admin/directory/v1/users/"+email+"?fields=id";
var token = ScriptApp.getOAuthToken();
var header = {"Authorization":"Bearer " + token};
var options = {
"method": "GET",
"headers": header
};
var response = JSON.parse(UrlFetchApp.fetch(apiUrl, options));
return response;
}
which I run as a function from App Maker project. Things go smoothly when I use the app since I have an admin role( I guess, not sure ) but the problem arises when other normal users in our domain start using the deployed app maker app. I checked the server logs and its full of this message:
Exception: Request failed for
https://www.googleapis.com/admin/directory/v1/users/email#domain.com?fields=id
returned code 403.
Truncated server response: { "error": { "errors": [ { "domain": "global",
"reason": "forbidden", "message": "Not Authorized to access this
resource/api" ... (use muteHttpExceptions option to examine full response)
Any idea how to fix this? I have manually added the required scopes for the apps script library, I added the following:
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/admin.directory.user"
The reason this happens is because YOU have admin rights, otherwise you'd be getting the same error message. The other users don't have admin rights hence they get the error. To solve this problem, you can either deploy the application running it as the developer or you can use a service account to impersonate an admin and do the process.
Regarding the first approach, you can find more info here https://developers.google.com/appmaker/security/identity.
Regarding the second approach, you can use the following app script library https://github.com/gsuitedevs/apps-script-oauth2#using-service-accounts
Moreover, if you do not require to get custom schemas information, then you can simply use a directory model and that should work for all users. Check the reference here: https://developers.google.com/appmaker/models/directory
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?)