Google Drive API - Searching for files within folders - google-drive-api

This question is not how about to search within folders, but why I'm not getting the correct results.
This is how I'm formatting my search request which is basically a search for "mount" inside two folders.
GET: https://www.googleapis.com/drive/v3/files?q=fullText contains 'mount' AND ('{folder_id}' in parents OR '{folder_id}' in parents)
Headers:
Authorization: Bearer {access_token}
Which gets me this response:
{
"kind": "drive#fileList",
"incompleteSearch": false,
"files": []
}
When I make the request with code, I don't get any results nor when I use an API client to manually send the request. But, whenever I use the "Try it now" in the Google Drive files list API I do get results... which is perplexing to me since I'm using the exact same query there (copy/pasting). Can anyone see what I'm doing wrong?
I'm using these permissions
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.appdata
Things I've tried:
Encoded the full query, with and without encoding the single quotation marks.
Copying the full encoded query from the "Try it now" tool.
Reduced the number of folders to search through down to one (it works in this case, anything more than one results zero results).
Changed the position of the search and the folder query.
Lowercased "AND" and "OR" inside the query.

In order to correctly understand your situation, I asked the following 4 questions.
The account of {access_token} of Authorization: Bearer {access_token} is the same as the account you tested But, whenever I use the "Try it now" in the Google Drive files list API I do get results?
When you tested "Try it now", you used other parameters except for q?
As a test case, when you use only the scope of https://www.googleapis.com/auth/drive and retrieve the access token again and test your request, what result will you obtain?
When you test the following curl command by replacing ### with your access token and placing folderId1 and folderId2 with your folder IDs, what result will you obtain?
curl -H "Authorization: Bearer ###" "https://www.googleapis.com/drive/v3/files?q=fullText%20contains%20%27mount%27%20AND%20(%27folderId1%27%20in%20parents%20OR%20%27folderId2%27%20in%20parents)"
As the result, from the section 1, it was found as follows.
I went through your questions again very carefully and I realized that I'm using a Google Service Account and not the main account. So, technically, it was not the same account even though the Google Service Account had access to some folders of the main account, but not all of folders.

Related

How do get the url "https://script.google.com/macros/d/{SCRIPT ID}/usercallback" responding / up and running

I'm a beginner in Google Apps Script.
I'm basically trying to use the OAuth service in a Google Apps Script to be able to connect a Google spreadsheet to Yahoo fantasy API.
I'm using the package you can found here:
https://github.com/googleworkspace/apps-script-oauth2
In the "read me" part you can see that oauth libraries will by default use the callback url https://script.google.com/macros/**d**/{SCRIPT ID}/usercallback
So I try to use this package in my script, but when I publish my script it creates the following url:
https://script.google.com/macros/**s**/{SCRIPT ID}/
Notice it's an "s" instead of the "d" in the url.
I can access properly to https://script.google.com/macros/**s**/{SCRIPT ID}/exec and so on but the url containing the "d", which is expected to be use by OAuth service, return an error.
When trying to access the url in a browser I got:
an error if I call ../usercallback
the message: "Sorry, the file you've asked doesn't exist", if I call the ../exec
.. is https://script.google.com/macros/**d**/{SCRIPT ID} in that case.
I've been looking on internet for having this url working but no success (it that so obvious that no one else had the issue except me?).
I tried several thing, i've created a project and linked my script to that project, i played a bit with the configuration in the project in regards to what i found on internet, like credential for API, but still can't have the url with a "/d/" responding.
I'm not sure of the meaning of the "s" (script?) or the "d" in the url.
I'm not sure if it has something to do with the way I publish my script or if I need to have specific function in my script or maybe some configuration of the script, maybe I need to persevere with the project thing...
I think that you should re-read the Readme.md file from the Google OAuth Sample GitHub repo. Also I think that d and s parts of Google Apps SCript URLs are irrelevant for using the "OAuth Service".
The .../exec URL is generated when making a new deployment. This URL doesn't contain the script Id... please bear in mind that a single Google Apps Script project might be deployed multiple times and have all or some deployments being live at the same time. Each web app deployment will have it's own URL... none of them have the script id. By the other hand, you might not need to make a deployment in order to be able to use "the OAuth service", this is needed only if the script will be receiving GET/POST HTTP requests.
You should not to access the ../usercallback in the browser, as you will be doing a GET request without proving the required data. This URL should be added when getting the credentials to be used with the "OAuth Service" from the OAuth provider, in this case Yahoo, as the redirect URI.
The referred repo includes multiple samples about how to use the "OAuth Service", one of the is Yahoo.gs
Resources
https://developers.google.com/apps-script/guides/web
https://developers.google.com/apps-script/concepts/deployments

Google Drive API failing - Unusual traffic from your computer network

The Google Drive API GET query started to fail (we are using this api in one of our client service):
https://www.googleapis.com/drive/v3/files/1ke4Yoxxxxxxxxxxxxxx?alt=media&access_token=ya29.ImG9BwT.....
We're sorry...
... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now.
See Google Help for more information...
"Unusual traffic from your computer network"
Everything was ok till today.
Just made a couple of tests with curl:
HTTP GET with access_token inside HTTP header:
curl -H "Authorization: Bearer ya29._valid_access_token" https://www.googleapis.com/drive/v3/files/1r5BT2WPrulQ6FyhT8RcqV51TVOThEmhK?alt=media
Result: success, file downloaded.
HTTP GET with access_token as a part of HTTP request:
curl https://www.googleapis.com/drive/v3/files/1r5BT2WPrulQ6FyhT8RcqV51TVOThEmhK?alt=media&access_token=ya29._valid_access_token
Result: error
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}
Note that the Google API project approved by Google and has a Production state!!!
Hi Google team, any ideas why it is happening?
Thank you
Posting this just for documentation purposes. As per the reference provided by Tanaike, since January 1, 2020:
download calls to files.get, revisions.get and files.export endpoints which authenticate using the access token in the query parameter will no longer be supported.
Only requests that download media content (alt=media) are affected by this change.
The access token should be provided in the HTTP header, like Authorization: Bearer oauth2-token or, if that's not possible, follow the workarounds provided in the referenced documentation:
For file downloads, redirect to the webContentLink which will instruct the browser to download the content. If the application wants to display the file to the user, they can simply redirect to the alternateLink in v2 or webViewLink in v3.
For file exports, redirect to the export link in exportLinks with the desired mime type which will instruct the browser to download the content.
Reference:
Changes in authorization to Google Drive API
Authorization via HTTP header
Posting an addition to the documentation of iamblichus specific for application data in Google Drive.
My App uses the Google Drive API in the backend to store the documents of my users on their own Drive. Importantly, I store the data in an application data folder such that the users can't accidently delete the data.
To let the users retreive their documents I used to redirected the user to the
downloadUrl: https://www.googleapis.com/drive/v2/files/id?alt=media&source=downloadUrl&access_token=ya29.** where I added alt=media and access_token to the query. This worked fine till January 1, 2020. As confirmed above. However, now the request fails with the message:
We're sorry... ... but your computer or network may be sending
automated queries. To protect our users, we can't process your request
right now.
GET request downloadUrl failing
In my efforts to find a solution to this problem I've tried the workarrounds provided by Google but they are in my experience not working for documents in an application data folder.
I tried the webContentLink with the access_token query in v2 but it fails with an 401 Unauthorized error.
The alternateLink in v2 and the webViewLink in v3 fail with the error:
The desired file does not exist.
I can't use the exportLinks because that's only for Google Documents.
The solution I found was to first download the file to the server using the Google Drive PHP SDK and then serve it to my users using a Content-Dispostion header. Such as described here. It is a lot less elegant in my opinion because the end users doesn't download the document directly from the Google Drive API but it works.
It's a shame that Google didn't update their documentation nor notified their users..

How to sync my pictures from FlashAir to Google Drive?

based on this answer: https://photo.stackexchange.com/a/92941/71846
I'm trying to set up my new Toshiba FlashAir to sync with my Google Drive account.
The instructions are here: http://www.fixstars.com/en/news/?p=451
However, I'm having trouble with part 1 of the second step (I suspect the steps are actually out of date).
I've left out the information in the form-data because my Google API Key is there.
From the instructions, it mentions: "NOTE: Google tells you to use “https://www.googleapis.com/auth/drive” for scope, but this will return a “Invalid_scope: Not authorized to request the scope”. Using /feeds/ instead will grant us the Google Drive authorization we need."
So I also tried using “https://www.googleapis.com/auth/drive” for scope but I'm getting the same "invalid request" back in the JSON response.
I thought that in your question, there might be a problem at the process for retrieving refresh token. So how about using Drive API? The following flow is from part 1 to part 3 of the sample for Google Drive of https://photo.stackexchange.com/a/92941/71846 in your question. This flow retrieves the refresh token for using Drive API.
Flow :
On your browser.
Create new project at https://console.developers.google.com/
At "Library" of https://console.cloud.google.com/apis/library/drive.googleapis.com/?project=### project ID ###, enable Drive API.
Go to "Credential" of https://console.cloud.google.com/apis/credentials?project=### project ID ###.
Create Credential
Select "Help me choose"
At "Which API are you using?", select "Drive API".
At "Where will you be calling the API from?", select "Other UI (e.g. Windows, CLI tool)".
At "What data will you be accessing?", select "User data".
Click "What credentials do I need?"
At "Name of Create an OAuth 2.0 client ID", Input name.
Click "Create Client ID".
Click "Done." By this, back to Credentials screen.
Click the created credentials.
Copy "Client ID" and "Client secret"
Please input your clientId and access https://accounts.google.com/o/oauth2/auth?client_id=### clientId ###&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/drive
By authorizing, the code is retrieved.
Copy "code".
On your terminal.
Retrieve refresh token using curl command. Please input your clientId, client secret and retrieved code.
curl -X POST https://accounts.google.com/o/oauth2/token -d "client_id=### clientId ###" -d "client_secret=### client secret ###" -d "redirect_uri=urn:ietf:wg:oauth:2.0:oob" -d "grant_type=authorization_code" -d "code=### code ###"
Note :
If you have already enabled Drive API and retrieved client ID and client secret, you can start from 5. If that didn't work, please start from 1.
If I misunderstand your question, I'm sorry.

Box API get shared file results in 404

Already looked at How to use box-api to get the Shared item? and Box Developers documentation https://developers.box.com/docs/#shared-items. I have tried doing a request to https://api.box.com/2.0/shared_items in .NET code, in Firefox Poster and using curl. They all resulted in 404. I have verified that my file's shared link is open to anyone with a link. Here's my curl code (sanitized):
curl https://api.box.com/2.0/shared_items -H "Authorization: Bo
xAuth api_key=xxxxxxxxxxxxxxxxxxxxx" -H "BoxApi: shared_link=https://
app.box.com/s/yyyyyyyyyyyyyyyyyyyyyyyyyyyy"
{"type":"error","status":404,"code":"not_found","help_url":"http:\/\/developers.
box.com\/docs\/#errors","message":"Not Found","request_id":"196207167555469c3017
14b"}
What might be the problem? I tried a wrong api_key and got back 401 so I know the whole setup is correct.
Your API key shouldn't be included anywhere in the request. I believe that form of authorization was used in the old v1 API and is now deprecated. There's a cURL example in the docs you linked to that shows what your authorization header should look like:
curl https://api.box.com/2.0/shared_items
-H "Authorization: Bearer ACCESS_CODE"
-H "BoxApi: shared_link=SHARED_LINK"
For more info on how to obtain the access token that goes into the authorization header, take a look at the OAuth tutorial.
The reason why authentication is required is because the API will return info about the item within the context of the caller's permissions. For example, if the creator of the shared link makes the API call, then private information (such as the item's full path within their account) will be returned. If another user makes the call, then that private information will be omitted.
That being said, you're still able to anonymously download an open shared link by navigating to it directly. You only need to use the API if you want more information about the item that the link points to.

GET/POST requests Google Drive API

I learning currently Google Drive API and developing program in Qt C + + (using OAuth2), so I need to build queries, but I'm not found how to do it.
For example, if I make a request - https://www.googleapis.com/oauth2/v1/userinfo?access_token=MY_ACCESS_TOKEN, everything is working OK - I get the reply.
Question is: how to make a SIMILAR request for the Google Drive?
1) how can I get a list of folders and files
2) how can I create a folder / file
etc.
For example, in a POST request
"https://www.googleapis.com/drive/v1/files&title=pets&mimeType=application/vnd.google-apps.folder"
I get
"error": {
"errors": [{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}
how to get a list of folders and files, for example, etc., I do not understand
Any opinions/examples are welcome!
thanks in advance
Unfortunately, the Drive API doesn't let you list folders or files. The only way to retrieve a file is by integrating with the Drive web UI or showing a Google Picker to your user (web app only).
Once you have a File ID, you can simply send an authorized GET request to the drive.files.get endpoint:
GET https://www.googleapis.com/drive/v1/files/id
To insert a file (or a folder), the File's metadata is to be included in the request body, not as query parameter:
POST https://www.googleapis.com/drive/v1/files
Authorization: Bearer {ACCESS_TOKEN}
Content-Type: application/json
...
{
"title": "pets",
"parentsCollection": [{"id":"0ADK06pfg"}]
"mimeType": "application/vnd.google-apps.folder"
}
In the example above, the mimeType specifies that the resource being inserted is a folder. Change the mimeType to your application MIME type when inserting a file.
You should use google docs API for that
Retrieving a list of collections - Google Documents List API version 3.0
In OAuth 2.0 Playground just select both Documents List and Google Drive
And you will get full list of operations:
List FolderContent
GET https://docs.google.com/feeds/default/private/full/{folderResouceId}/contents
Don't forget to add the ?v=3 query parameter to the Document list URI or adding
GData-Version: 3.0
header, otherwise it will return "Invalid request URI".
You advise you to look at the google drive api reference docs in
https://developers.google.com/drive/v3/reference/files/list
i succeeded using many api functions following link above and using curl. But actually i recommend you trying to do http requests with a tool like postman. Anyway after getting access code following the steps link below
list google drive files with curl
try to make following command in the command line.
curl -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \
https://www.googleapis.com/drive/v2/files?maxResults=$100