Help

External access token authorization working differently in Airtable than in Google Apps Script?

Solved
Jump to Solution
5957 10
cancel
Showing results for 
Search instead for 
Did you mean: 
DPG
6 - Interface Innovator
6 - Interface Innovator

I can't seem to get a valid access token in Airtable based on JavaScript code that works in a Google Apps Script. Here is the working code:

 

  var clientId = '...';
  var clientSecret = '...';
  var url = 'https://accounts.spotify.com/api/token';
  var headers = {
    'Authorization': 'Basic ' + Utilities.base64Encode(clientId + ':' + clientSecret)
  };
  var payload = {
    'grant_type': 'client_credentials'
  };
  var options = {
    'method': 'post',
    'headers': headers,
    'payload': payload
  };
  var response = UrlFetchApp.fetch(url, options);
  var accessToken = JSON.parse(response.getContentText()).access_token;

 

This code relies on objects and methods that don't exist in Airtable's automation coding environment, namely Utilities.base64encode() and UrlFetchApp.fetch(), so I have tried rewriting the script to work in Airtable. To bypass an encoding method, I simply encoded the clientId and clientSecret manually and substituted the encoded string for Utilities.base64encode(clientId + ':' + clientSecret). In order to fetch the access token, I have tried a few lines of declaring variables. This is what the Airtable script looks like:

 

var url = 'https://accounts.spotify.com/api/token';
var headers = {
  'Authorization': 'Basic ' + '...'}; // '...' is the encoded string
var payload = {
  'grant_type': 'client_credentials'};
var options = {
  'method': 'post',
  'headers': headers,
  'payload': payload};

var responseToken = await fetch(url, options);
var dataToken = await responseToken;
var accessToken = await responseToken.access_token;

 

Have I approached this incorrectly? I appreciate any help.

10 Replies 10
DPG
6 - Interface Innovator
6 - Interface Innovator

@saadat, Airtable's JavaScript environment doesn't support the Utilities.base64Encode() method, which is why the solution relies on a custom function, btoa(), to encode.