I have not been able to figure out how to do the same from Google scripts or find an example. Any ideas?
This is the sample code from Google that I believe is applicable:
getRequest(url, params)
Returns the request that would be made if the operation were invoked. This method does not actually issue the request.
// The code below logs the value for every key of the returned map.
var fields = {'in_reply_to_screen_name': true, 'created_at': true, 'text': true};
function tweet() {
// Setup OAuthServiceConfig
var oAuthConfig = UrlFetchApp.addOAuthService("twitter");
oAuthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
oAuthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
oAuthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize");
oAuthConfig.setConsumerKey(ScriptProperties.getProperty("twitterConsumerKey"));
oAuthConfig.setConsumerSecret(ScriptProperties.getProperty("twitterConsumerSecret"));
// Setup optional parameters to point request at OAuthConfigService. The "twitter"
// value matches the argument to "addOAuthService" above.
var options =
{
"oAuthServiceName" : "twitter",
"oAuthUseToken" : "always"
};
var result = UrlFetchApp.getRequest("https://api.twitter.com/1.1/statuses/user_timeline.json",
options);
for(i in result) {
Logger.log(i + ": " + result[i]);
}
}
And sends the corresponding values in column 2 to the variables with IDs in column 1:
var TOKEN="AmUU5BccbQdKx4aXm4XZAIisb5Gddy";
/**
* Post data to Ubidots using the Token
* @param {variable} id of the variable for post data.
* @param {payload} object with de data ex:{"value":23}
* @return the Reponse
*/
function postUbidots(variable, payload){
var options =
{
"contentType" : "application/json",
"headers" : {"X-Auth-Token": TOKEN},
"method": "post",
"payload": JSON.stringify(payload)
};
var response = UrlFetchApp.fetch("http://things.ubidots.com/api/v1.6/variables/"+variable+"/values",
options);
return response;
}
/**
* Post data to Ubidots using the Token
* @param {payload} collection for post data: http://ubidots.com/docs/api/v1_6/collections/post_values.html
* @return the Reponse
*/
function postCollectionUbidots(payload){
var options =
{
"contentType" : "application/json",
"headers" : {"X-Auth-Token": TOKEN},
"method": "post",
"payload": JSON.stringify(payload)
};
var response = UrlFetchApp.fetch("http://things.ubidots.com/api/v1.6/collections/values",
options);
return response;
}
/**
* Retrieves all the rows in the active spreadsheet that contain data and logs the
* values for each row.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var data = [];
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row.length == 2){
data.push({"variable": row[0], "value": row[1]});
}
}
postCollectionUbidots(data);
};
/**
* Post data to UBIDOTS.
*
* @param {number} input The value to post.
* @return The input multiplied by 2.
* @customfunction
*/
function POST_UBIDOTS(input) {
return input * 2;
}
function onOpen() {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Post Data', functionName: 'readRows'}
];
spreadsheet.addMenu('Ubidots', menuItems);
}
The code creates a menu in the tool bar that says Ubidots.
var TOKEN="pKD17DNsSZ6QRCCS9pWIC5Uo2pn3Wn";
var variable = "547881c2762542609d265fe4";
var payload = {value : 30.0};
/**
* Post data to Ubidots using the Token
* @param {variable} id of the variable for post data.
* @param {payload} object with de data ex:{"value":23}
* @return the Reponse
*/
function postUbidots(variable, payload){
var options =
{
"contentType" : "application/json",
"headers" : {"X-Auth-Token": TOKEN},
"method": "post",
"payload": JSON.stringify(payload),
"muteHttpExceptions" : false
};
var response = UrlFetchApp.fetch("http://things.ubidots.com/api/v1.6/variables/"+variable+"/values",
options);
return response;
}
Sure, you’re getting the “Not Found” error because the API can’t find the resource you are sending, in this case the variable. This is because “variable” is defined in more than one place, so any of these solutions should work: