UrlFetchApp function not working with arrays - google-apps-script

I'm using this IMPORTJSON script I found on Github to retrieve data from the Facebook Graph API.
It was working fine until I started using filtering arrays in the URL.
For example, this URL works fine :
https://graph.facebook.com/v10.0/account_id/insights?&level=campaign&fields=campaign_name,campaign_id,actions&access_token=access_token&date_preset=last_week_mon_sun
but when I try to retrieve data from the URL below, I get the following error = "Exception: Invalid argument" :
https://graph.facebook.com/v10.0/account_id/insights?&level=campaign&fields=campaign_name,campaign_id,actions&filtering=[{field:"campaign.id",operator:"IN",value:[12345678910]}]&access_token=access_token&date_preset=last_week_mon_sun
After troubleshooting, I think that the problem comes from the UrlFetchApp used in the script.
Is there anyway to format the URL to avoid getting an error ?

Solution found : I used the encodeURIcomponent function to encode (You have to use it on the parameters only, not on the whole URL !)
Eg : encodeURIComponent([{field:"campaign.id",operator:"IN",value:[12345678910]}])

Related

How do I code a doGet(e) function to get an array of name/values?

I have a Web App example, from a tutorial using doPost(e), which uses var body = JSON.parse(e.postData.contents) to append rows from the request to a Google sheet.
However I want to use doGet(e), but I haven't yet found out how to write the equivalent code.
I found this answer that says URL parameters are received in doGet under a "parameter" property of the first function parameter
So e.parameter.name would be the format.

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 :)

Google Apps Script: How to encode the "parameters" param in the Execution API

I am trying to pass some arguments to a script that is deployed as API executable. Unfortunately, for the language I use, there is no Google client library. So I have to construct the POST request myself. Everything is fine except that I can't figure out how to encode the parameters param. The request body must be URL encoded so I tried all of these but none works:
function=generateDoc&devMode=true&parameters=aaa
function=generateDoc&devMode=true&parameters="aaa"
function=generateDoc&devMode=true&parameters=["aaa"]
function=generateDoc&devMode=true&parameters[]="aaa"
function=generateDoc&devMode=true&parameters=%22aaa%22
function=generateDoc&devMode=true&parameters=%5B%22aaa%22%5D
function=generateDoc&devMode=true&parameters%5B%5D=%22aaa%22
And as a result my script function doesn't see the passed argument. Reports undefined. Please, help! Thanks.
I found the answer - the entire request must be JSON encoded and then not URL encoded. Also application/json content type must be supplied. This request works for me
{"function":"generateDoc","devMode":true,"parameters":["aaa"]}
For a discussion on Google+ https://plus.google.com/116965811397164811393/posts/ECkp2E6kU1a

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.

Google dictionary api does not return pure json?

I have the following code:
var json = new WebClient().DownloadString(string.Format(#"http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q={0}&sl=en&tl=en", "bar"));
This returns something like this:
dict_api.callbacks.id100({"query":"bar","sourceLanguage":"en","targetLanguage":"en","primaries":[{"type":"headword","terms":[{"type":"text","text":"bar",....
Why is it returning a function rather than just the json? Am I using web client incorrectly?
As I understand it, this is JSONP - JSON which is "padded" with a function call to allow cross-domain data transfer. I strongly suspect that if you pass in a different callback name on the URL, you'll see that other name come back in the response.
(Note that although I work for Google, this answer is not an "official" response from Google in any way, shape or form.)
You may want to use check this out :
json_decode for Google Dictionary API
They actually modify the resultant jsonp to get a json