I use a very cool Google Script I found on Github (source: https://gist.github.com/beezly/9b2de3749d687fdbff3f) to fetch the temperature on my Nest thermostat and log it into a Google Spreadsheet.
It is working great when I run the script manually, but not when I use a time trigger to run it automatically.
When posting the credential to the API on this line:
Line 12: var response = JSON.parse(UrlFetchApp.fetch('https://home.nest.com/user/login', options).getContentText());
The exception raised is:
Request failed for https://home.nest.com/user/login returned code 429.
Truncated server response: Too many requests (use muteHttpExceptions
option to examine full response) (line 12, file "Code")
I suspect that I'm not the only one using the script, and the Google source IP used by the trigger has exceeded the limit of the Nest API.
Would you have any solution for me? I have considered using a Proxy server, but the UrlFetchApp.fetch function does not seem to accept it.
Thank you,
PF
I only ran into the "Too many requests" error during development when I was testing that same script. I was able to get this function to run hourly no problem, even every 5 minutes.
To get this script run as expected on a trigger just switch the getData() to a doGet(), publish it as a web app with anonymous access then create another function (i called it getData()) that makes a request to your published web app url (which invokes a doGet()), then just create your time-based trigger to run the new getData().
here's my version https://gist.github.com/jbutters/bece2fffe85080fe4314
Related
I have been attempting to find a method to log any errors that occur after a doPost(e) method executes with Apps Script (using JS). The previously answered question regarding this topic mentioned StackDriver logging with reference to the old editor provided by Google. However, how do I access a similar function with the new editor?
For some more context, if I execute functions from script editor, the Google Sheet they are editing, or through a trigger, I am able to view the errors through the Execution Log tab. But for any calls made through doPost, I am unable to view the causes of error. How do I log these errors?
Edit: I believe there might be a slight misunderstanding. The programmed doPost(e) takes user input from a Telegram bot and responds accordingly. Till date, the bot has been functioning as designed; however, for future debugging I wish to be able to somehow see any caused errors. This would provide more information than simply a "Failed" in the execution log. Any help is appreciated.
Apps Script Logging
Note:
Error Reporting interface in the Developer Console is pretty much similar with option 2 so I will not provide any further example.
Sample WebApp Code:
function doGet(e) {
var params = JSON.stringify(e);
console.log(params);
}
Sample Request:
https://script.google.com/..../exec?username=sample2&password=1234
Option1: Check the built-in Apps Script execution log
You can check the logs when you open your script and go to Execution tab
You can also check the logs in https://script.google.com/home/executions
Option2: Using Cloud Logging interface in the Developer Console
Assuiming you already attached a standard GCP project with your script project
You can view Cloud logs and error reports in the Google Cloud Platform console
Under Log Fields -> Select Apps Script Function
It will display the results under Query Results Tab
Before I get into the details, the basic error is that a json response from postman for making requests to the Apps Script API Either returns a success or failure solely depending on the version of the script I have selected as an API executable.
Here are the details:
I am attempting to remotely execute a google apps script function through the API through postman. When publishing a test script that I am using to test our the API functionality as an API executable as a certain version, as seen here:
When making a post request to execute a function which returns nothing from this version of the script, I get the following response:
This response doesn't contain an ExecutionResponse object, presumably because my function doesn't return anything. I wanted to see if I could return data, though, so I updated this function to return a simple "helloWorld!" in version two of the script. When re-publishing as an API Executable, this time with version two published:
The postman response is this:
I can confirm that the error is reproducible solely by switching between versions of my script (and whether I've produced new credentials to reflect the API executable change doesn't change the response); that is the only variable changing between the two responses; no matter if I create new credentials after changing the version or not, the same difference in response is produced. What is accounting for the error here?
I'm hoping to automate some HR work by running a Google App Script via the Execution API. Without getting too much into the details, I'd like to pass employee evaluation data as a parameter into the App Script. The script will then use this data to compile an "Employee Review" GDoc.
So far, I have ran a simple test App Script using the Execution API. For example, I can successfully run a simple function which logs a string or interacts with spreadsheets. So far so good.
But I run into problems when trying to write to a GDoc (which is unfortunately integral to my task). Here's my paired down script:
// TODO: Eventually, we'll pass these variables as arguments
var docId = "MY-DOC-ID";
// Find the team member review doc
var doc = DocumentApp.openById(docId);
// Replace placeholder text
var docBody = doc.getActiveSection();
docBody.replaceText('{{DATE}}', "Date set by App Script!!!");
doc.saveAndClose();
This script works when I press the "Run" button in the App Scripts web UI. But when I try to run via the Execution API, I get:
{
"error": "unauthorized_client",
"error_description": "Unauthorized client or scope in request."
}
So apparently I haven't provided the correct scope? Following the docs, I can find the necessary scope(s) in Project Properties > Scopes which says:
But when I try adding that scope, it wont work. As I said other scopes (e.g. https://www.googleapis.com/auth/spreadsheets) work just fine. Perhaps the auth/documents scope is no longer supported or there's a bug in their API?
Questions
What is the correct scope? I can see a big list here but I don't see https://www.googleapis.com/auth/documents, so?
Any other suggestions? For example, is it possible to write to a Google Doc using the Google Client API directly (i.e. without using App Scripts)?
Doh. I figured out the solution to my problem. While it was a dumb mistake, it's nevertheless worth posting as it may save others confusion in the future.
First, a little context about my setup. I'm authenticating to the Google Client API using a Service Account. Furthermore, as is common when using a service account setup, I am impersonating a user within our organization (specifically my own account).
My missing step (obvious in hindsight)...
Log into the App Script web UI as the person you are impersonating.
Manually run the script by pressing the play button
If the impersonated user has not already granted permissions to access the required scopes, you will be prompted to do so.
After granting access (specifically for the https://www.googleapis.com/auth/documents scope), my authorization error disappeared.
So the lesson: Make sure the account you are impersonating has granted access for all the scopes which your script requires.
I have a Google spreadsheet with a script to fetch Fitbit data that is working fine, but any other ones I have, which are the same in the parts that matter give me this error when I try to authorize:
"We're sorry, a server error occurred. Please wait a bit and try again"
I have determined by using the logger that this occurs at the line:
var result = UrlFetchApp.fetch("https://api.fitbit.com/1/user/-/profile.json", options);
The whole code is here: https://jsfiddle.net/j72qbe08/ (this is mostly copied from code I found by searching online)
The code for the other existing spreadsheet that is still working also has the same error when I copy it into a new spreadsheet and try to authorize. But I wasn't starting from a new spreadsheet when I first had this error, it was an existing sheet that was working fine recently.
It's supposed to open a dialog saying "Authorization required" and then a window allowing you to log in to Fitbit. But it's not doing any of that.
Presumably it's something with the Google script editor because the other spreadsheet still works.
Thank you
EDIT: According to this page Google has deprecated support for OAuth1 and it's scheduled for shut down on April 20. It's only April 15, but maybe they did it early? Not sure if that explains anything. Fitbit API still not OAuth2 compatible
EDIT 2:
"February 23, 2015: We will revoke a progressively larger number of access tokens on an ongoing basis, until most users are affected."
Guess that might explain it.
If fitbit api only uses Oauth1 you can use this Oauth1 library to make the connection.
https://github.com/googlesamples/apps-script-oauth1
The readme.md shows how to set it up.
I have some GAS currently being run off of a spreadsheet. They will eventually move to a published service, however, the current issue needs to be resolved first.
I'm running into an issue where a script that I've written, and other users have used in the past are receiving the Subject error message.
A new script I can run (but others cannot) has been taken down to a single test function
function hello()
{ Browser.msg("Hello There") }
This still generates the "You do not have authorization to perform that action."
My older script has had no script changes and now users receive the same error.
I hadn't updated the Sharing settings on my script. Maybe that's what Jan was referring to...
Thanks Jan !