How to use access token in a JavaScript script?

Hey everyone!

Right now I'm trying to use the access token from a REST resource I built with a custom OAuth2 auth flow. The script is meant to run with information of a selected row in a table, and it needs the access token for the task.

I built the custom OAuth2 auth and I'm saving the access token as a variable so I can use it in the request headers and to refresh the token, but I haven't been able to get that variable into the app itself.

Here is the auth flow and the refresh auth flow.

The script with needs the token to run is supposed to run when I click this button of the table, but that is working ok.

Hey @AlejandroPalacio! Welcome to the community forums. :slight_smile:

You mentioned that your auth end point is the same as your resource endpoint, right? I've given this some thought since we last talked and I think the following should work:

  1. Define your REST resource with custom authorization. Note that you can define when this auth pattern runs in the resource settings:
  2. Within your app, define a query that connects to the resource you defined above and that takes in a parameter.
  3. Add an event handler to your button that triggers this query with additional scope - specifically with the selected row. You can see an example of that here.

This way, your resource queries should all have access to the token and to the state of your app - your table's selectedRow, in this case. You're also in control of how frequently you authorize the resource, so it won't need to go through the entire auth flow with every query if you don't want it to.

I hope that helps! Don't hesitate to follow up here or drop by office hours if you have further questions.

Were you able to resolve this issue, @AlejandroPalacio? We're still here to help out, if not!