weird file listing response differences between v2 and v3 - google-drive-api

I am using the google-drive-sdk with our company-made device. We upload pictures made by our device to google drive. After that I try to list the files with a GET request to https://www.googleapis.com/drive/v2/files to get thumbnailLink and webContentLink. Everything is working fine except that when I switch to v3 I don't get the response I should. The documentation says I should get a metadata response like https://developers.google.com/drive/v3/reference/files
but I only get: id, kind, name and mimeType. What am I doing wrong?

As stated in Migrate to Google Drive API v3 documentation, there are changes on how fields were returned.
Full resources are no longer returned by default. You need to use the fields query parameter to request specific fields to be returned. If left unspecified only a subset of commonly used fields are returned.
You can see examples on Github. This SO question might also help.

In v3 they made all the queries parametric. So you can query passing some parameter like
var request = gapi.client.drive.files.list({
'pageSize': 10,
'fields': 'files,kind,nextPageToken'
});
This block of code will return you all the information of every file just like v2.
If you are sending a get request then for fetching all the information you can try GET https://www.googleapis.com/drive/v3/files?fields=files%2Ckind%2CnextPageToken&key={YOUR_API_KEY}
Suppose you need ownsers and permissions only then set
var request = gapi.client.drive.files.list({
'pageSize': 10,
'fields':'files(owners,permissions),kind,nextPageToken'
});
For GET request use GET https://www.googleapis.com/drive/v3/files?fields=files(owners%2Cpermissions)%2Ckind%2CnextPageToken&key={YOUR_API_KEY}
for reference you can use Google Developers Documentation for fetching File list

Related

Twitter API for Non-public/organic/promoted metrics in Google Script

I'm trying to use the Twitter API in Google Script to get the non-public metrics of my posts.
I believe have done all of the authentication properly since I am able to get the information when using Postman. I have generate a consumer_key, a consumer_secret, an access_token, and a token_secret.
According to the twitter documentation , I MUST use Oauth1.0 (https://developer.twitter.com/en/docs/twitter-api/metrics).
In order use the Oauth1, I used Google's own script for Twitter Oauth1 (https://developers.google.com/google-ads/scripts/docs/examples/twitter-oauth10).
I was able to take that code (along with the library it required) and successfully retrieve the tweets for my username.
My attempt to get the non_public metric was replacing the "https://api.twitter.com/1.1/statuses/user_timeline.json" with the GET request I had from POSTMAN but it returned a "401 error".
I have a hunch that I can't just replace that url with my own but I am unsure how to approach it.
In summary:
Tokens/Twitter Authorization/GET request are written properly since they work in Postman
Google Script is writing properly since the default URL works
Unsure how to replace the default url to accomplish different tasks
Ended up figuring it it out!
I was not supposed to pass the entire url as a replacement for "https://api.twitter.com/1.1/statuses/user_timeline.json".
It is supposed to be replaced by the "base" url. In my case this was "https://api.twitter.com/2/tweets".
I then had to change the variable "params" that feed into it.
note that the params is from the code provided by Google in my original post
They have to be in the following format (https://developer.twitter.com/en/docs/tutorials/twitter-api-google-sheets):
params = {
"tweet.fields": "author_id,created_at,lang",
"ids": "21,1293593516040269825,1334542969530183683",
}
I was able to add my own fields such as "non_public_metrics,organic_metrics" to get:
params = {
"tweet.fields": "author_id,created_at,lang,non_public_metrics,organic_metrics",
"ids": "21,1293593516040269825,1334542969530183683",
}
I hope this helps someone someday :)

Unable to search on specific boolean fields on files' properties in Google Drive API v3

I'm trying to use the Google Drive API to list certain files and am using the q parameter on files().list() method. I'm able to get the fields by specifying fields=nextPageToken, files(id...,shared) but when using q="shared" or q="shared = true", I get the following error Invalid Value:
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/files?pageSize=1000&fields=nextPageToken%2C+files%28id%2C+name%2C+parents%2C+modifiedTime%2C+kind%2C+mimeType%2C+size%2C+trashed%2C+ownedByMe%2C+shared%29&q=shared+%3D+true&alt=json returned "Invalid Value">
Is the above even allowed ? Because the fields I'm trying to compare against aren't present in the File Query Terms table at https://developers.google.com/drive/api/v3/ref-search-terms
Are the query terms provided on the API reference exhaustive and I can only perform searches on those terms ?
Unfortunately, in the current stage, shared is not included in the fields for the search query. Ref I think that the reason of your error is due to this.
Although there is sharedWithMe in the fields, this retrieves the file list of shared: true and ownedByMe: false. I thought that this might not be the result you expect. So when you want to retrieve the files with shared: true and ownedByMe: true, it is required to retrieve them after the file list was retrieved.
So, for example, how about reporting it as the future request to the Google issue tracker? Ref By this, Google side might consider to add new fields in the search query. Unfortunately, when I searched the same report, unfortunately, I couldn't find it.
Reference:
Search for Files

Posting multiple JSON files at onece

I am working with an api (Track-pod) and uploading JSON files to their server using a google apps script. I know this question has probably already been answered, but I have searched google extensively and couldn't find an answer, or maybe I just wasn't typing in the right keywords. Each Json file that I am uploading contains information on customers for the company I am working for. The way I am doing it is like so
for each(var item in array)
{
option.payload = JSON.stringify(item);
UrlFetchApp.fetch(url, option);
}
In my code the array is an array of objects for each customer. I was wondering if I have to constantly make requests, or is there a way to upload all the JSON files at once. Or at least make it faster.
To save some time you can use UrlFetchApp.fetchAll(). It will take an array of request as parameter and you can do up to 10 requests at the same time if I well remember.
Don't forget to check destination endpoint limit to not over charge it.
Reference : https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchAll(Object)
Stéphane

In Google Apps Script is the command “AdminDirectory.Groups.insert(group)” considered to be a post request?

In Google Apps Script I am calling the G Suite Admin SDK Directory API using the command AdminDirectory.Groups.insert(group). Is this considered to be a POST request? I’m wondering because I know there are implicit ways of making POST requests where POST is not specified explicitly such as urlfetch(). In the project that I am working on, I am trying to avoid using HTTP requests for security reasons.
I did some research online, but I am having some difficulty finding the answer to this question. I'm thinking that since a JavaScript object rather than a JSON object was passed into insert(), it would not be considered a POST request since JSON notation is typically used when sending data to a server or retrieving data from a server. Because group is a JavaScript object and not a JSON object, I am thinking the command AdminDirectory.Groups.insert(group) would not be a POST request. Am I on the right track here?
For some context, here is the function I wrote to create a group:
function createAGroup() {
var group = {
email: "test-group#test.com",
name: "Test Group",
description: "This is a test group."
};
group = AdminDirectory.Groups.insert(group);
Logger.log('Group %s created.', group);
}
The function createAGroup() created a group successfully. However, was the command AdminDirectory.Groups.insert(group) using a POST request to create the group or not?
If you are not sure which kind of request you are looking at - you can find it out in the Google Developers Reference.
In your case:
If you go to the Apps Script Reference for the admin directory:
https://developers.google.com/apps-script/advanced/admin-sdk-directory
It will link you to the reference documentation for the specific methods for the Admin SDK Directory API where you can find the reference for the specific method Groups:insert
https://developers.google.com/admin-sdk/directory/v1/reference/groups/insert
It tells you:
HTTP request
POST https://www.googleapis.com/admin/directory/v1/groups
This syntax is translated one to one into Apps Script, which you can prove as following:
If you test the method in “Try this API” with your request body, the outcome will be:
200
{
"kind": "admin#directory#group",
"id": "03oy7u293zlw6l7m",
"etag": "\"zPBZh0mDALCYqI567HUiXii8qQjpg/VckrVGnV8Hs56iDrqRt7j4XT5eRyM\"",
"email": "test-group#test.com",
"name": "Test Group",
"description": "This is a test group",
"adminCreated": true
}
Now if you run it in Apps Script your Looger.lo output will be:
Group {kind=admin#directory#group, name=Test Group, description=This is a test group., etag="zPBZh0mDALCYqI7HMkUiXii8qQjpg/gIcr9tsZMDRRrDJECvLtNT66KBc", id=00ha3apch11zp6hh, adminCreated=true, email=test-group#test.com} created.
You can see that in both cases the response retrieves the data in the same way and gives you an equivalent feedback. Thus, it is safe to say that the method used in App Script, uses indeed the POST request.
As a general rule:
Anything that is creating a new object at the backend (like inserting users or groups) is a POST request, anything that is updating existing objects (e.g. change personal data of a user) is a PUT request, anything that retrieves data (e.g. listing users) is a GET request and DELETE is pretty self-explaining.
App Script is a “tool” that takes away the need to implement the request manually. But always check the reference how to implement a specific method.
If you do want to make an explicit JSON request - you can convert the JavaScript notation into a JSON string with JSON.stringify() as explained here:
https://developers.google.com/apps-script/guides/services/external#work_with_json

Angular JSONP request

I'm trying to get a jsonp response from an api call api.brewerydb.com. However, it's not wrapping the json with a function on its call back. Here is my code:
app.factory('beer', ['$http',function($http){
var url = "http://api.brewerydb.com/v2/beers?key=MYKEY&application/json&name=oberon&callback=JSON_CALLBACK";
$http.jsonp(url)
.success(function(data){
return data;
});
}]);
It is returning json data, however I get a syntax error at a file whose name is the url I passed and it contains the json data.
I discovered that the api does not support jsonp which is why this isn't working out for me
Your url may be incorrect: '../application/json..' is for an optional HTTP_ACCEPT header per their api docs here: breweryDB , so remove it from the var url string if its not needed.
Per their docs, the default return type is JSON, so no need to pass an extra parameter if its not needed.
Try this instead (for the endpoint /Beers) per their endpoint doc :
var url = "http://api.brewerydb.com/v2/beers?key=MYKEY&name=oberon&callback=JSON_CALLBACK";
Go get them beers!
EDIT: code is here: http://jsfiddle.net/r47y3mq3/1/ copy/paste to your local dev environment and use Safari. Using Chrome results in an Access-Control-Allow-Origin header concern as discussed here: access-control
I was having some similar issues. You have name=oberon which is a brewery, but you are searching for beers in your url.
If you tried:
var url = "http://api.brewerydb.com/v2/breweries?key=MYKEY&application/json&name=oberon&callback=JSON_CALLBACK";
...you'd get an array of all the beers in the Oberon brewery.