authorization - Google docs spreadsheet script error OAuthConfig when fetching data from Fitbit -
i have got following script in google docs spreadsheet fetching data fitbit. script worked fine far on 6th july google stopped using oauthconfig script not working since:-(
i not programmer, advanced user. kindly ask programmer tune script below in order make work again.
// key of scriptproperty firtbit consumer key. var consumer_key_property_name = "fitbitconsumerkey"; // key of scriptproperty fitbit consumer secret. var consumer_secret_property_name = "fitbitconsumersecret"; // default loggable resources (from fitbit api docs). var loggables = ["activities/log/steps", "activities/log/distance", "activities/log/activescore", "activities/log/activitycalories", "activities/log/calories", "foods/log/caloriesin", "activities/log/minutessedentary", "activities/log/minuteslightlyactive", "activities/log/minutesfairlyactive", "activities/log/minutesveryactive", "sleep/timeinbed", "sleep/minutesasleep", "sleep/minutesawake", "sleep/awakeningscount", "body/weight", "body/bmi", "body/fat",]; // function authorize() makes call fitbit api fetch user profile function authorize() { var oauthconfig = urlfetchapp.addoauthservice("fitbit"); oauthconfig.setaccesstokenurl("https://api.fitbit.com/oauth/access_token"); oauthconfig.setrequesttokenurl("https://api.fitbit.com/oauth/request_token"); oauthconfig.setauthorizationurl("https://api.fitbit.com/oauth/authorize"); oauthconfig.setconsumerkey(getconsumerkey()); oauthconfig.setconsumersecret(getconsumersecret()); var options = { "oauthservicename": "fitbit", "oauthusetoken": "always", }; // profile force authentication logger.log("function authorize() attempting fetch..."); try { var result = urlfetchapp.fetch("https://api.fitbit.com/1/user/-/profile.json", options); var o = utilities.jsonparse(result.getcontenttext()); return o.user; } catch (exception) { logger.log(exception); browser.msgbox("error attempting authorization"); return null; } } // function setup accepts , stores consumer key, consumer secret, firstdate, , list of data elements function setup() { var doc = spreadsheetapp.getactivespreadsheet(); var app = uiapp.createapplication().settitle("setup fitbit download"); app.setstyleattribute("padding", "10px"); var consumerkeylabel = app.createlabel("fitbit oauth consumer key:*"); var consumerkey = app.createtextbox(); consumerkey.setname("consumerkey"); consumerkey.setwidth("100%"); consumerkey.settext(getconsumerkey()); var consumersecretlabel = app.createlabel("fitbit oauth consumer secret:*"); var consumersecret = app.createtextbox(); consumersecret.setname("consumersecret"); consumersecret.setwidth("100%"); consumersecret.settext(getconsumersecret()); var firstdate = app.createtextbox().setid("firstdate").setname("firstdate"); firstdate.setname("firstdate"); firstdate.setwidth("100%"); firstdate.settext(getfirstdate()); // add listbox select data elements var loggables = app.createlistbox(true).setid("loggables").setname( "loggables"); loggables.setvisibleitemcount(4); // add possible elements (in array loggables) var logindex = 0; (var resource in loggables) { loggables.additem(loggables[resource]); // check if resource in getloggables list if (getloggables().indexof(loggables[resource]) > -1) { // if so, pre-select loggables.setitemselected(logindex, true); } logindex++; } // create save handler , button var savehandler = app.createserverclickhandler("savesetup"); var savebutton = app.createbutton("save setup", savehandler); // put controls in grid var listpanel = app.creategrid(6, 3); listpanel.setwidget(1, 0, consumerkeylabel); listpanel.setwidget(1, 1, consumerkey); listpanel.setwidget(2, 0, consumersecretlabel); listpanel.setwidget(2, 1, consumersecret); listpanel.setwidget(3, 0, app.createlabel(" * (obtain these @ dev.fitbit.com)")); listpanel.setwidget(4, 0, app.createlabel("start date download (yyyy-mm-dd)")); listpanel.setwidget(4, 1, firstdate); listpanel.setwidget(5, 0, app.createlabel("data elements download:")); listpanel.setwidget(5, 1, loggables); // ensure controls in grid handled savehandler.addcallbackelement(listpanel); // build flowpanel, adding grid , save button var dialogpanel = app.createflowpanel(); dialogpanel.add(listpanel); dialogpanel.add(savebutton); app.add(dialogpanel); doc.show(app); } // function sync() called download desired data fitbit api spreadsheet function sync() { // if user has never performed setup, if (!isconfigured()) { setup(); return; } var user = authorize(); // spatny kod, oprava nize - var doc = spreadsheetapp.getactivespreadsheet(); var doc = spreadsheetapp.getactivespreadsheet().getsheetbyname('pavel'); doc.setfrozenrows(1); var options = { "oauthservicename": "fitbit", "oauthusetoken": "always", "method": "get" }; // prepare , format today's date, , list of desired data elements var datestring = formattoday(); var activities = getloggables(); // each data element, fetch list beginning firstdate, ending today (var activity in activities) { var currentactivity = activities[activity]; try { var result = urlfetchapp.fetch("https://api.fitbit.com/1/user/-/" + currentactivity + "/date/" + getfirstdate() + "/" + datestring + ".json", options); } catch (exception) { logger.log(exception); browser.msgbox("error downloading " + currentactivity); } var o = utilities.jsonparse(result.getcontenttext()); // set title var titlecell = doc.getrange("a1"); titlecell.setvalue("date"); var cell = doc.getrange('a2'); // fill spreadsheet data var index = 0; (var in o) { // set title column var title = i.substring(i.lastindexof('-') + 1); titlecell.offset(0, 1 + activity * 1.0).setvalue(title); var row = o[i]; (var j in row) { var val = row[j]; cell.offset(index, 0).setvalue(val["datetime"]); // set date index cell.offset(index, 1 + activity * 1.0).setvalue(val["value"]); // set value index index index++; } } } } function isconfigured() { return getconsumerkey() != "" && getconsumersecret() != ""; } function setconsumerkey(key) { scriptproperties.setproperty(consumer_key_property_name, key); } function getconsumerkey() { var key = scriptproperties.getproperty(consumer_key_property_name); if (key == null) { key = ""; } return key; } function setloggables(loggable) { scriptproperties.setproperty("loggables", loggable); } function getloggables() { var loggable = scriptproperties.getproperty("loggables"); if (loggable == null) { loggable = loggables; } else { loggable = loggable.split(','); } return loggable; } function setfirstdate(firstdate) { scriptproperties.setproperty("firstdate", firstdate); } function getfirstdate() { var firstdate = scriptproperties.getproperty("firstdate"); if (firstdate == null) { firstdate = "2012-01-01"; } return firstdate; } function formattoday() { var todaydate = new date; return todaydate.getfullyear() + '-' + ("00" + (todaydate.getmonth() + 1)).slice(-2) + '-' + ("00" + todaydate.getdate()).slice(-2); } function setconsumersecret(secret) { scriptproperties.setproperty(consumer_secret_property_name, secret); } function getconsumersecret() { var secret = scriptproperties.getproperty(consumer_secret_property_name); if (secret == null) { secret = ""; } return secret; } // function savesetup saves setup params ui function savesetup(e) { setconsumerkey(e.parameter.consumerkey); setconsumersecret(e.parameter.consumersecret); setloggables(e.parameter.loggables); setfirstdate(e.parameter.firstdate); var app = uiapp.getactiveapplication(); app.close(); return app; } // function onopen called when spreadsheet opened; adds fitbit menu function onopen() { var ss = spreadsheetapp.getactivespreadsheet(); var menuentries = [{ name: "sync", functionname: "sync" }, { name: "setup", functionname: "setup" }, { name: "authorize", functionname: "authorize" }]; ss.addmenu("fitbit", menuentries); } // function oninstall called when script installed (obsolete?) function oninstall() { onopen(); }
problem solved updated code @ https://github.com/loghound/fitbit-for-google-app-script
Comments
Post a Comment