I am trying to PATCH (partially update) Firebase records (firebase.com). This works perfect outside of Google Apps Script using PATCH. GAS is not supporting PATCH and I tried X-HTTP-Method-Override without success. Using GAS with X-HTTP-Method-Override renders the same result as a standard GET. There is no security on my test database. No log-in is required.
var myPayload = "{\"WSD124\" : {\"auction\" : {\"stockno\" : \"ESD124\", \"highbid\" : \"240\"}}}";
var myURL = "https://mydatabase.firebaseio.com/auctions/.json";
var options = {
headers: {
"X-HTTP-Method-Override" : "PATCH"
},
method: "POST",
payload: myPayload
};
var oResponse = UrlFetchApp.fetch(myURL,options);
A quick update for those coming back to this old thread - we now support X-HTTP-Method-Override headers on all REST API calls, so this should now work
UPDATE
This is now supported. See Chris Raynor's answer.
OLD ANSWER
We don't currently support X-HTTP-Method-Override though we are considering it. For now you'll likely have to do a PUT with the whole record. Give us an email at support#firebase.com if this is significantly blocking you.
Related
I tried to login my account by using UrlFetchApp, when I ran script manually, everything worked well. But when I set trigger to run automatically, it returned "error_need_ivs". I have no idea what it is and how to fix. please help me. Below is my code
function login() {
var data = {
'username':'username',
'password_hash':'password',
}
var option = {
muteHttpExceptions: true,
"method" : "POST",
"payload" : data,
}
var url = 'my url'
var res = JSON.parse(UrlFetchApp.fetch(url,option).getContentText());
Logger.log(res) //returned error_need_ivs when I set trigger but worked well when I ran script manually
}
UPDATE : This picture show the result after scripting executed by trigger
From the new screenshot shared I can see no Exceptions were thrown, which means the error logged error_need_ivs certainly comes from the response of the HTTP request rather than an issue with Apps Scripts.
Regarding why it works when calling the affected function manually and why it doesn't worked when invoking it via timed-trigger, I believe what #Tanaike suggested to make sense, maybe the affected endpoint is not prepared to receive IPv6 requests or require more data when doing so.
I'd suggest checking the documentation for the endpoint you are making the request for to help identify what might be causing this behavior.
Alternatively, I'd also suggest to report it as an Issue on IssueTracker using this template. Make sure to include a full reproducible scenario when reporting it (including a working endpoint).
I got an error when I try to get the content of a file (mimetype is "text/csv") stored in my Google drive using Drive.Files.get(fileId,{alt: 'media'}) from a Google apps script (I'm using Google Drive API / REST V2).
The strange thing is that the content of my file seems to be returned inside the message of the error, as follow:
{
"message":"Response Code: 200. Message: id\turl\tthumbnail_url\ttitle\tpublishedAt\tdescription\tchanelId\tcategoryId\ttags\tduration\tdislikeCount\tviewCount\tlikeCount\tcommentCount\r\n....",
"name":"HttpResponseException",
"fileName":"Code",
"lineNumber":142,
"stack":"\tat Code:142 (updateChannelVideos)\n",
"statusCode":200
}
Do you know how I can get the content of my file, from the server side, without using service like UrlFetchApp?
It appears that Google Apps Script can't handle a response from var myFile = Drive.Files.get(fileID,{alt: 'media'}); and if not already you might want to file as a bug.
Edit: updated related issue ticket here.
Instead of using the Drive Advanced Service you might find it easier to use DriveApp. Depending on the rest of your code no additional scopes would be required when mixing Drive and DriveApp calls.
For example you could use:
var myFile = DriveApp.getFileById(fileID).getAs('text/plain').getDataAsString();
I bypassed this issue using:
var fetchstring='https://www.googleapis.com/drive/v2/files/'+FILE_ID
fetchstring=fetchstring+'?mimeType=text/csv&alt=media';
var file_to_upload = UrlFetchApp.fetch(fetchstring, {
method: "GET",
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}}).getBlob();
Adapat the mimetype / getBlob parts to your use case
I am developing an add-on for Google Docs and I want to make POST request to my web server from add-on. I have already done that, but how should I validate on server-side that the request is coming from my add-on only? Is there csrf like mechanism in Google App Script? If not, any workaround to it?
There is a direct method in Apps Script to get UUID : Utilities.getUuid()
Reference : https://developers.google.com/apps-script/reference/utilities/utilities#getuuid
For memory previous answer below.
There is not mechanism for that but the best way is to add in the post request a specific key. Like API key in Google, example : 94e631ba-9916-4490-a084-cde08dcc0757
For generating a key example here : https://codepen.io/corenominal/pen/rxOmMJ
Adapted code below :
function generateUUID()
{
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c)
{
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
}
Then on your server your check this value. If API Key is valid you perform the request if not you return a 403.
If you want you can implement an OAuth flow to connect to your server like Google do for its API but from my point of view it is faster to use an API key. If you combine 2 key like the one above probability to find it is near 0.
Stéphane
I am sending push notifications to my Android phone. I want these notifications grouped so my notification list does not get flooded.
According to the documentation messages from the same 'source' get grouped but on my phone the messages always show up ungrouped.
I call the push API from a Google Apps script and have tried setting source_device_iden, source_user_iden and notification_tag when I call the push API. None of these seem to make any difference.
How can I get the pushmessages to be grouped on my phone?
Google Apps script code
function pushNoteToPhone(title, body) {
var digest = "Basic "+Utilities.base64Encode(PUSH_BULLET_TOKEN+":");
var options = {
"method" : "post",
"payload" : {
"device_iden" : MYPHONE_ID,
"type" : "note",
"title" : title,
"body" : body,
"source_device_iden" : <device id>,
"notification_tag": "tag1",
},
"headers" : {
"Authorization": digest
}
};
var push_bullet_url = "https://api.pushbullet.com/v2/pushes";
UrlFetchApp.fetch(push_bullet_url, options);
}
The easiest way to do this (admittedly it should be easier) is to create an OAuth Client and then send using an access token for that oauth client. That way the pushes will all appear to come from that client instead of you. This is how IFTTT and Zapier work on Pushbullet.
Here's how to setup an oauth client: https://docs.pushbullet.com/#oauth
To get an access token you can use the "oauth test url" on the create client page, you will end up with an access token in the URL once you approve access. Use that access token instead of your normal one and the pushes will appear to come from the client instead of you.
Don't know how you are trying to update the notification but without the code, my guess is that you are trying to pass a new Notification ID to each notification being sent to the device. However, please take a look here and look under, "Updating Notifications". As explained in the documentation, by passing the same ID to each notification it will either group these notifications on the device or create a new one in case the old one has been dismissed.
With the recent news that ScriptDB is being deprecated, I'm searching for a suitable replacement. My particular use case is that I'm running Google Apps Script under Google Forms to process and store data relevant to the function of the form.
I've been through Google's migration guide (link), and I'm trying to connect a Google Apps Script running under a Google Form to a Parse Database (link). I've tried both methods listed on the migration guide (URL Fetch Service and ParseDB Library), and I can't get either to work correctly. I was able to write to the Parse Database using ParseDB, but the query function isn't working as expected. I also tried using the parseCom library from the Excel Liberation site (sorry, I'm out of links for this post, apparently), but that didn't work very well either.
I'm most interested in using Google's URL Fetch Service to connect to a Parse database, as that seems to be my most flexible option (i.e. to let me share data between forms - something I really couldn't do with scriptDB), but I feel like I'm in over my head just a bit. I'm open to other options as well. Thanks in advance!
I have the same 'query' problem using the MongoLab database as overstack-asked here. I thought Parse might work as an alternative, but then I saw this post having the same 'query' problem.
If you run your query from the browser directly to Parse it will probably work fine, just as it does for me using the MongoLab database.
Consequently, I strongly suspect the problem is in the URLFetchApp.fetch() function itself, not the Parse (nor MongoLab) database functions.
Here's what I have found to be the best way to query results using UrlFetchApp and Parse.
function query(key,value) {
var properties = getKeys();
var appId = properties.appId;
var restApi = properties.restApi;
var class = 'TestObject';
var url = 'https://api.parse.com/1/classes/' + class;
var query = 'where={"' + key + '":"' + value + '"}'
var encoded = encodeURIComponent(query);
var queryUrl = url + '?' + encoded;
var options = {
"method" : "get",
"headers" : {
"X-Parse-Application-Id": appId,
"X-Parse-REST-API-Key": restApi,
}
}
var data = UrlFetchApp.fetch(queryUrl, options);
return data;
}
I am also looking for this.
As a temp store ScriptDb has been very helpful. Sheets simply does not have the elegance of beautiful coding as in ScriptDb