Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: store token in userscriptProp object #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions index.gs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ function getSheetNumbers() {
return questionNumbers;
}

function getToken(baseUrl, username, password) {
function getToken(baseUrl) {
var username = Browser.inputBox('enter metabase user name', Browser.Buttons.OK_CANCEL);
# FIXME: i couldn't find any password input. this needs a workaround
var password = Browser.inputBox('enter metabase password', Browser.Buttons.OK_CANCEL);
var sessionUrl = baseUrl + "api/session";
var options = {
"method": "post",
Expand Down Expand Up @@ -216,11 +219,10 @@ function getQuestionAndFillSheet(baseUrl, token, metabaseQuestionNum, sheetName)
}
} else if (statusCode == 401) {
var scriptProp = PropertiesService.getScriptProperties();
var username = scriptProp.getProperty('USERNAME');
var password = scriptProp.getProperty('PASSWORD');
var userscriptProp = PropertiesService.getUserProperties();

var token = getToken(baseUrl, username, password);
scriptProp.setProperty('TOKEN', token);
var token = getToken(baseUrl);
userscriptProp.setProperty('METABASE_TOKEN', token);
var e = "Error: Could not retrieve question. Metabase says: '" + response.getContentText() + "'. Please try again in a few minutes.";
return {
'success': false,
Expand Down Expand Up @@ -261,14 +263,14 @@ function fillSheet(values, sheetName) {

function getQuestionAsCSV(metabaseQuestionNum, sheetName) {
var scriptProp = PropertiesService.getScriptProperties();
var baseUrl = scriptProp.getProperty('BASE_URL');
var username = scriptProp.getProperty('USERNAME');
var password = scriptProp.getProperty('PASSWORD');
var token = scriptProp.getProperty('TOKEN');
var userscriptProp = PropertiesService.getUserProperties();

var baseUrl = scriptProp.getProperty('METABASE_BASE_URL');
var token = userscriptProp.getProperty('METABASE_TOKEN');

if (!token) {
token = getToken(baseUrl, username, password);
scriptProp.setProperty('TOKEN', token);
token = getToken(baseUrl);
userscriptProp.setProperty('METABASE_TOKEN', token);
}

status = getQuestionAndFillSheet(baseUrl, token, metabaseQuestionNum, sheetName);
Expand Down