andrewkittredge
andrewkittredge

Reputation: 850

Access UserProperties in customfunction?

We want to get data from our API into Google Sheets. Our API requires authentication. Our tentative architecture has the user entering their credentials in a ModalDialog, then we hit our authentication endpoint which returns a session token which is saved. Subsequently the user makes customfunction calls in which we make calls to our API data end-point and include the authentication token.

My question - How do I get the authentication token into the scope of the customfunction? It seems like PropertiesService.getUserProperties should do this but the values we set in the function that gets the credential token are not visible in the customfunction.

function saveCredentials(action){
     var username = action['username'],
         password = action['password'];
     var response = UrFetchApp.fetch('https://www.foo.com/account/logon/, {method: 'post', payload: {email: action, password: password}});
     var token = response.getAllHeaders()['Set-Cookie'];
     PropertiesService.getUserProperties().setProperty('token', token);}


/**
* @param {parameter1} 
* @param {parameter2}
* @customfunction
*/
function getDataFromFooAPI(parameter1, parameter2){
    var token = PropertiesService.getUserProperties().getProperty('token') // expecting this to be the value set in saveCredentials but it is not.
    var url = 'https://www.foo.com/api/data/' + parameter1 + '/' + parameter2;
    var response = UrlFetchApp.fetch(url, {'headers' : {'token' : token}};
}

Does the context in which the customfunction executes references a different PropertiesService than the rest of the script? Are we trying to do the impossible?

Upvotes: 0

Views: 176

Answers (1)

Steve Bazyl
Steve Bazyl

Reputation: 11692

There are some documented limitations with custom functions, notably that they don't have access to personal information. They can access script and document properties, but not user properties. Custom functions should behave consistently for all users who have access to the sheet and operate without authorization. Relying on user properties would make that virtually impossible.

If you can use document properties instead that should work.

Upvotes: 1

Related Questions