How can a chain access a JSON value received by another chain?
I have two chains, one (A) which makes a call to an API to retrieve an access token (embedded in the returned JSON) and a second (B) which I want to be able to consume that token and use it with a target system. (There are several use cases requiring a token, which is why I want to centralize the retrieval of a token in a standalone chain.)
I assumed this would be straightforward to achieve using the Run Chain command. I configured chain A to get the token, then configured chain B to first run chain A, then use the JSON response in a downstream command in chain B. But when I tried to transform that JSON response via "Get values from JSON" to extract and use only the token itself, instead of opening up the transformation interface as usual, I instead got the message "No transformations can be added to this variable." Upon further investigation, it appears the response from chain A is passed into chain B as a file reference only, which I suppose explains why I can't easily transform it.
I shrugged and went back to chain A, figuring I just needed to extract the token there and properly export it for downstream use. Because I had provided the output schema to the HTTP POST command in chain A, it was easy to define a dynamic variable for the chain and set it in the node after the POST to be equal to the extracted access token. But that dynamic variable does not seem to be available to chain B, so that approach didn't work, either.
After reading the article Getting Started with Variables I wondered if I could stow the token in a workspace variable, as I thought perhaps this is what this comment was getting at:
Common examples of Workspace variables include: API base URLs and access tokens (Client ID and Client Secrets that are generated after creating the OAuth2 Grant). Using Workspace Variables is helpful when you’re needing to connect to an API in order to retrieve source data and you need to create multiple chains that use the same API.
I'm already using workspace variables to store most of the parameters for the POST call, so I thought, sure, why not have chain A store the token in a workspace variable, then let chain B retrieve it? But I don't see any way for a chain to assign a value to a workspace variable.
I see two other options (but I'm skeptical of each, so I thought I'd post this question before I try them). First, I know there is also the "dynamic output" option within the POST command where I could extract the access token. But this would require me to use a regex (more effort) and, more importantly, I suspect that—like a dynamic variable—this output would not be available to chain B, either. Second, I know I could process the response file that chain B has access to and somehow extract the access token (doing something hopefully less weird and insecure than JSON file to CSV to Workiva sheet back to chain, for example), but at that point the benefits of this design pattern seem to not be worth the time and complexity.
Nevertheless, I feel certain there must be an easy, obvious way to do this, but I can't seem to think of what it is. Can anyone suggest what I should try instead?
-
Just checking again -- anyone have any ideas or input on this?
0For anyone who stumbles upon this: thanks to some help, I learned that an easy solution to this problem is using the Handlebars connector to create a template referencing the JSON; you can then create a chain output in the first chain (A, in my original example) and set it to the value of the template. That chain output is then accessible to the consuming chain.
0Accedi per aggiungere un commento.
Commenti
2 commenti