Skip to content

Use cookies #10

Open
Open
@oshliaer

Description

@oshliaer

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;

}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions