Open
Description
Cookie handling in Google Apps Script - How to send cookies in header?
function example() {
Logger.log(getContacts());
}
// Some API
function getContacts() {
auth();
var headers = {
'Cookie': CacheService.getUserCache().get('auth')
};
var params = {
'headers': headers,
muteHttpExceptions: true
};
return UrlFetchApp.fetch( /* API URL */ , params);
}
// Auth flow
function auth() {
var cache = CacheService.getUserCache().get('auth');
if (!cache)
auth_();
}
function auth_() {
var params = {};
params.method = 'post';
params.payload = {
LOGIN: 'USER LOGIN',
PWD: 'USER PASSWORD'
};
var fetch = UrlFetchApp.fetch( /* API URL */ , params);
CacheService.getUserCache().put('auth', fetch.getAllHeaders()['Set-Cookie'], /* IF IT EXPIRES */ );
return fetch;
}