Server error in Google Apps script for fetching Fitbit data - google-apps-script

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.

Related

Can not run trivial app script bound to new blank document - get "This app is blocked"

I am unable to run any app script in my google account - even one created by me bound to a new document created by me. When I do, I get an "This App is Blocked" error. Note that this is different from the the "Sign in temporarily disabled for this app" failure mode mentioned elsewhere, and the solution for that problem has no effect on this one.
To test, I created a new spreadsheet while logged into my personal google account and put the value ONE in cell A1...
I then opened the Tools->Script editor from the menu bar of this spreadsheet and entered the following trivial script...
I then click on the run icon from the script editor menu bar with the function "myFunction" selected from the pulldown.
I get an "authorization required" popup and so I click "Review Permissions"...
I then get a "Choose an account" popup and click on my account (the only logged in account, and the same account I was logged into when I created the sheet and the script)...
I then get an "App Blocked" popup. Checking the execution log shows that the script did not run.
What is causing this popup and how can I prevent this so I can run app scripts?
NOTE: I see many other questions describing more complicated versions of this issue but none have useful answers. I am hoping this ultra simple version of the issue will help narrow it down and get a resolution.
More unexpected behavior:
If I put the code into the onLoad() function it works fine and never even asks me for authorization.
If I enter this code:
...and then quit out and reload the sheet, then I get this in the execution logs...
Again, this is with no authorization popups at all. The sheet loads without interruption and then the entry is in the execution logs. So this would seem to not be a case of not having the right permissions somewhere since code clearly can access the sheet.
Legacy editor
Identical behavior under the legacy editor...
Other accounts
I can repeat these exact same steps on a different google account and it works fine, so this problem appears to be linked to my account. Many others on the internet have noted the same finding. Seems like there might be some hidden (probably unintional) setting attached to the account that gets switched somehow and then thereafter the account is not able to manually authorize scripts to run.
Changing Project
I check and this script is in the "Default" project as expected...
According to this page,
For most applications and scripts, you never need to see or adjust this default GCP project—Apps Script handles all the necessary interactions with the Google Cloud Platform automatically.
Since I have nothing to lose, tried creating a new project in the Google Cloud Platform console, and then tried assigning this script to that new project. Unfortunately when I tried, I got the normal "Authorization needed" popup which lead to this opdd page...
Clicking on the "Troubleshoot this problem" link takes me to this page...
...which seems to say that I do not have the permissions to troubleshoot problems on my own account.
This again seems to suggest there is something misconfigured about my account on google servers. :/
Unfortunately there are no good answers there. Lots of people like me wake up one random morning to find that they can not run any new App Scripts in their account any more.
I've found a workaround that works great... but will make you very sad.
This issue does not affect code that runs automatically, so functions like onOpen() and onEdit() run just fine and have full access to the bound document. You heard that right- google blocks code that the user explicitly requests to run to protect their data, but code that runs silently and automatically anytime a sheet is opened or modified is free to run and access (and change!) whatever data it wants.
So to let the user run your code, you pick a cell inside the spreadsheet that either has a value that changes whenever you want your code to run, or you make a special cell called "Edit this cell to run the program".
Then you put your code inside the onEdit() and (if desired) check to see to see if the special cell was updated. If so, then run your arbitrary code. It has full access to the spreadsheet and can read and update cells at will and can also write to the log.
Note that you must close the sheet and re-open it for the code to take effect.
Here is what my demo spreadsheet looks like...
...and here is the demo code...
function onEdit(e) {
var range = e.range;
const triggerCell = "B2";
if( e.range.getA1Notation() === triggerCell){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var rangeB1 =sheet.getRange("B1");
var rangeB2 =sheet.getRange("B2");
var date = Utilities.formatDate(new Date(), Intl.DateTimeFormat().resolvedOptions().timeZone, "HH:mm:ss");
rangeB2.setValue("Code ran at " + date );
Logger.log( date + ": B1=" + rangeB1.getValue() );
}
}
Here is a demo video...
https://youtu.be/ypuLaUWn1R8
I've said it before, I'll say it again - if you were thinking about using Google Cloud Services for anything then think again. This is crap built on top of crap that no one at google understands and it occasionally breaks suddenly and catastrophically, and there is no one who can even tell you what is going on much less how or when or if it is going to get fixed.
I ran into this for the first time today but my issue seems to be due to:
created a sheet on a shared drive owned by a different organization
my organization uses the legacy apps script interface, the shared drive organization uses the new apps script interface
Using an account from the shared drive organization to create the script solved my issue.
This error
This app is blocked
seems to be a new error affecting certain Google accounts. This is reported to Google. Kindly star(add a star ★ to it on top left) to the following issues and comment to get Google developers to prioritize this issue:
https://issuetracker.google.com/issues/176138626
https://issuetracker.google.com/issues/181220763
Some workarounds:
#15 Creating a new GCP and linking it.
#12 Turning on Less Secure Apps
#10
Checking accounts security page
Turning off advanced protection
Unlock Captcha
This issue does NOT seem to be related to another issue 145162820:
"Sign in temporarily disabled for this app"
The error messages are different.

Unable to authorize ANY external Google Apps Script scope on any ANY file [duplicate]

I am unable to run any app script in my google account - even one created by me bound to a new document created by me. When I do, I get an "This App is Blocked" error. Note that this is different from the the "Sign in temporarily disabled for this app" failure mode mentioned elsewhere, and the solution for that problem has no effect on this one.
To test, I created a new spreadsheet while logged into my personal google account and put the value ONE in cell A1...
I then opened the Tools->Script editor from the menu bar of this spreadsheet and entered the following trivial script...
I then click on the run icon from the script editor menu bar with the function "myFunction" selected from the pulldown.
I get an "authorization required" popup and so I click "Review Permissions"...
I then get a "Choose an account" popup and click on my account (the only logged in account, and the same account I was logged into when I created the sheet and the script)...
I then get an "App Blocked" popup. Checking the execution log shows that the script did not run.
What is causing this popup and how can I prevent this so I can run app scripts?
NOTE: I see many other questions describing more complicated versions of this issue but none have useful answers. I am hoping this ultra simple version of the issue will help narrow it down and get a resolution.
More unexpected behavior:
If I put the code into the onLoad() function it works fine and never even asks me for authorization.
If I enter this code:
...and then quit out and reload the sheet, then I get this in the execution logs...
Again, this is with no authorization popups at all. The sheet loads without interruption and then the entry is in the execution logs. So this would seem to not be a case of not having the right permissions somewhere since code clearly can access the sheet.
Legacy editor
Identical behavior under the legacy editor...
Other accounts
I can repeat these exact same steps on a different google account and it works fine, so this problem appears to be linked to my account. Many others on the internet have noted the same finding. Seems like there might be some hidden (probably unintional) setting attached to the account that gets switched somehow and then thereafter the account is not able to manually authorize scripts to run.
Changing Project
I check and this script is in the "Default" project as expected...
According to this page,
For most applications and scripts, you never need to see or adjust this default GCP project—Apps Script handles all the necessary interactions with the Google Cloud Platform automatically.
Since I have nothing to lose, tried creating a new project in the Google Cloud Platform console, and then tried assigning this script to that new project. Unfortunately when I tried, I got the normal "Authorization needed" popup which lead to this opdd page...
Clicking on the "Troubleshoot this problem" link takes me to this page...
...which seems to say that I do not have the permissions to troubleshoot problems on my own account.
This again seems to suggest there is something misconfigured about my account on google servers. :/
Unfortunately there are no good answers there. Lots of people like me wake up one random morning to find that they can not run any new App Scripts in their account any more.
I've found a workaround that works great... but will make you very sad.
This issue does not affect code that runs automatically, so functions like onOpen() and onEdit() run just fine and have full access to the bound document. You heard that right- google blocks code that the user explicitly requests to run to protect their data, but code that runs silently and automatically anytime a sheet is opened or modified is free to run and access (and change!) whatever data it wants.
So to let the user run your code, you pick a cell inside the spreadsheet that either has a value that changes whenever you want your code to run, or you make a special cell called "Edit this cell to run the program".
Then you put your code inside the onEdit() and (if desired) check to see to see if the special cell was updated. If so, then run your arbitrary code. It has full access to the spreadsheet and can read and update cells at will and can also write to the log.
Note that you must close the sheet and re-open it for the code to take effect.
Here is what my demo spreadsheet looks like...
...and here is the demo code...
function onEdit(e) {
var range = e.range;
const triggerCell = "B2";
if( e.range.getA1Notation() === triggerCell){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var rangeB1 =sheet.getRange("B1");
var rangeB2 =sheet.getRange("B2");
var date = Utilities.formatDate(new Date(), Intl.DateTimeFormat().resolvedOptions().timeZone, "HH:mm:ss");
rangeB2.setValue("Code ran at " + date );
Logger.log( date + ": B1=" + rangeB1.getValue() );
}
}
Here is a demo video...
https://youtu.be/ypuLaUWn1R8
I've said it before, I'll say it again - if you were thinking about using Google Cloud Services for anything then think again. This is crap built on top of crap that no one at google understands and it occasionally breaks suddenly and catastrophically, and there is no one who can even tell you what is going on much less how or when or if it is going to get fixed.
I ran into this for the first time today but my issue seems to be due to:
created a sheet on a shared drive owned by a different organization
my organization uses the legacy apps script interface, the shared drive organization uses the new apps script interface
Using an account from the shared drive organization to create the script solved my issue.
This error
This app is blocked
seems to be a new error affecting certain Google accounts. This is reported to Google. Kindly star(add a star ★ to it on top left) to the following issues and comment to get Google developers to prioritize this issue:
https://issuetracker.google.com/issues/176138626
https://issuetracker.google.com/issues/181220763
Some workarounds:
#15 Creating a new GCP and linking it.
#12 Turning on Less Secure Apps
#10
Checking accounts security page
Turning off advanced protection
Unlock Captcha
This issue does NOT seem to be related to another issue 145162820:
"Sign in temporarily disabled for this app"
The error messages are different.

Your script, Drive Migrator, has recently failed to finish successfully

I have this email from google.
Your script, Drive Migrator, has recently failed to finish successfully. A summary of the failure(s) is shown below. To configure the triggers for this script, or change your setting for receiving future failure notifications, click here.
There are about 200 messages of the format:
Start 1/1/19 4:53 PM
Function processFilesAndFolders
Error Message Authorization is required to perform that action.
Trigger time-based
End 1/1/19 4:53 PM
When i click there i fund there is an empty trigger and no further information.
Project -
Last run -
Deployment -
Event Time-based
Function -
Error rate -
Its very strange given nobody was on the system at this time, and I cannot see any details about the trigger.
How do I see what is happening here? Is someone on my system, trying to move my files around? How is is possible to have a trigger that is void of information like this? It looks somewhat hacky. Can I centrally disable this "drive migrator"?
I found an old question about this: Drive Migrator Fail Message
But, perhaps it does not have enough information to get a proper answer.
I also contacted google support directly, but they said that asking here is the only way to get technical support on this service. I understand this is not the best "stackoverflow type question", but I'm at a loss on where to get further information about this.
The failure notifications likely occur when a Google Script is deleted from your Drive without de-authorization or when the permissions get revoked for some reason.
Go here and you'll find a list of all triggers running under your Google account. Delete the one associated with your Failure emails.
This guide explains how you can unsubscribe from the failure emails and also properly de-authorize the associated script.

GAS onOpen() not running for copied spreadsheet

I have a spreadsheet with a script that includes onOpen() which successfully creates some custom menus. When I copied the spreadsheet (and the bound script of course), the onOpen() no longer runs. I tried a few things I found from other postings of the same problem:
Revoke the new spreadsheet's authorization - can't because it does not show up in my account as an authorized app.
Manually create a trigger for when the doc opens - I get an error "We're sorry, a server error occurred. Please wait a bit and try again."
Copied the script file itself and renamed it.
Renamed the enclosing script project.
Renamed the enclosing spreadsheet.
So, none of these has worked. Anything else I can try, or is there a known problem on Google's side?
Thanks!
I looked at the said issue tracker and found that the problem stemmed from this line of code:
DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId());
To make this work, I would suggest to enable the Drive API in Resources -> Advanced Google Services. It will also ask you to enable Drive API in your Google Dev console. Do this and let me know if it worked for you.

We're sorry, a server error occurred. Please wait a bit and try again

I have several scripts bound to a spreadsheet that create 14 folders containing a total of about 240 google sheets. The original sheet that the scripts are bound to contains about 34000 rows of data that are divided among the newly created sheets. While I was working on it I frequently deleted and recreated all the folders and sheets and it worked fine. Yesterday, as I'm ready to do my final run and create the spreadsheets that will be shared and used I started getting "We're sorry, a server error occurred. Please wait a bit and try again". I tried again this morning and got the same thing.
I have tried a different browser. I created a new sheet and copied the scripts over. Then I deleted all but a single script that creates the folders and commented out all of the code in that except 3 lines that get the current spreadsheet and write out a message but I'm still getting the error.
The link to my stripped down, barely there but still not working spreadsheet is https://docs.google.com/spreadsheets/d/18_FjSpuGONEVzNZu5Ta8hm0YopsdlZlPRRAV7cx5VTE/edit#gid=1999362590
I'm thinking it may have something to do with my user account, or having reached some quotas. Is there any way to check that?
If you reach a quota when using a Google App Script, it should notify you with a specific exception. The quotas themselves can be seen here.
Have you tried removing the permissions for the script and authorising it again? I have had this issue previously and this resolved my problem (although, I accept it will not always resolve this problem...).
I would post the link, but I don't have enough reputation to post more than 2 links... Google "See apps connected to my Google account".