Callback URL for AirTable script

Hi, new scripter here in AirTable. I’m familiar with Google Apps Script, so I’m just trying to learn the minor differences here.

I’m trying to run a fetch POST operation here in Airtable scripting to a 3rd party site that stores jpgs. The fetch will return the image url and a bunch of metadata. I can retrieve this info in Apps Script ok. I’m trying to do the same here in AirTable.

My question before I get started is, the 3rd party company requires a callback URL from this source to allow access to their server. In Google Apps Script, it looks something like this;

https://script.google.com/macros/d//usercallback

What is the AirTable callback equivalent, so I can send that info the 3rd party company and get my access approved?

Thanks

Hi @Patrick_Maresco ,

If you do a fetch POST in Airtable script, you can run it asynchronously, so the script will wait for the 3rd party site to respond. Usually the response should contain the data you are after. Once you get response containing image URLs, you update attachment field in the record of your choice with the URLs.

Note that there is a 30 second time out on the length of running the script.

If you need the 3rd website to send something back to you after longer time or they are not sending data via response but via an additional call from their side, you could use Webbook in Automations to receive data:

Although when you specifically refer to “callback” and “getting access approved” it makes me think that you are looking at setting up some sort of OAuth connection?

Yeah, when I coded this in G Apps Script, I used the OAuth2 library. The 3rd party site wouldn’t let me access the content without logging in.

I’ll try to connect and see what happens. Maybe I won’t need to provide the callback URL for this. The original plan was to develop a webhook for this, but then I stumbled on airTable scripting, and thought I’d try this first.

I’m not sure I can make this work without setting up OAuth service. I need an Access Token to run my POST operation, but I can’t get that without first signing in to the account.

I’ll keep digging into this, but I’m not sure how to code this without credentials. I can figure out how to run fetch methods for sites that have open access (like google QR code generator, currency converter, etc), but how can I write this Airtable code to access a server with permissions?

Depending on your point of view, the differences aren’t necessarily minor. One of the big differences is that Airtable scripts cannot use libraries.

This is only true for automation scripts. There is no time limit for scripts run in Scripting Extension.

Sometime getting authenticated is the tricky part of using an API with Airtable scripting. Sometimes not having to deal with authentication headaches is a reason to use a third party service, like Make, Zapier, or Autocode.

Thanks @kuovonne I missed that part!

@Patrick_Maresco, so you it looks like you need to to do an OAuth, which depending on implementation is more or less painful. As @kuovonne mentioned 3rd party tools might be simpler to do the OAuth in a more permanent way. On top of the ones mentioned - I would also recommend Pipedream.com. It lets you execute JS code and has lower running costs vs Zapier / Make. They have also prebuilt Auth for a decent number of common services.

Well on the other hand if you would like to go full “option hard” you could technically do it in Airtable, using Airtable webhook as a callback. Here is example I tested on Google Youtube API (there will be variations in different implementations of OAuth though)

  1. Build a URL that will prompt user for consent to authenticate your app.

It will need to be fired “by hand” by the user to grant permission (you could just click on that link in Airtable cell)

https://accounts.google.com/o/oauth2/v2/auth
?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly
&access_type=offline
&include_granted_scopes=true
&state=state_parameter_passthrough_value_could_be_a_record_id
&redirect_uri=https://hooks.airtable.com/workflows/v1/genericWebhook/yourapp/webhookpath
&response_type=code
&client_id=yourclientID
&response_mode=form_post

Note that I added response_mode=form_post otherwise default method was GET and Airtable webhook at the moment accepts only POST

  1. Receive code on your webhook

  2. Use the script module to Exchange code for Token.

It would be something like this:

const {code} = input.config()

const reponse = await fetch("https://oauth2.googleapis.com/token",
{
    method: "POST",
    body:JSON.stringify({
        "code":code,
        "client_id":"YOUR_KEY",
        "client_secret":"YOUR_SECRET",
        "redirect_uri":"https://hooks.airtable.com/workflows/v1/genericWebhook/yourwebhook",
        "grant_type":"authorization_code"
    })
})

console.log(reponse.status,reponse.statusText)

const data = await reponse.json()

console.log(data)

Above will return object like this:

{
  "access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
  "expires_in": 3920,
  "token_type": "Bearer",
  "scope": "https://www.googleapis.com/auth/drive.metadata.readonly",
  "refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
}
  1. Getting data

You could now either:
a) make another request directly in the script above using the token for authentication - in case this is a one off situation
b) or save the token in Airtable table and reuse it for future requests.

There is obviously whole complexity with refreshing that token etc.

In short doable but I would personally prefer using a 3rd party service (like listed above) to set up OAuth connection.

I hope that helps!