Request template - Error while substituting templates with OAuth access_token in headers

I am writing a custom app that integrates to another platform using OAuth, I have managed to configure everything appropriately as described here and here, however, invoking the request method that uses oauth authentication fails with an error Error while substituting templates. on console logs when testing on my local machine. FDK debug logs show an error error while validating schema for {{templateName}} . Only the access_token is to be substituted in request schema’s header object, if I remove the access_token substitution and copy paste the token directly in the header schema, the request works as expected. The access_token is correctly fetched and stored in browser’s localstorage via OAuth since the token_type is agent. I am on the latest fdk version 9.0.1. I have checked other threads on this topic but it seems my issue is unique to them. I don’t know if there’s a limit to token length which could be causing the failure because our access_token is quite long otherwise I have run out of options to try make it work. Kindly advise urgently.

See attached images to back my little story above :slight_smile:



Screenshot 2023-05-19 at 10.00.19
Screenshot 2023-05-19 at 10.00.40

Hi @collin,

Welcome to the Freshworks developer community! :tada:

Where is this API request made in the app? Is it during the app installation or somewhere in the app flow?

Could you share the complete config/oauth_config.json file contents with secure credentials hidden?

Hi @Raviraj

The API request is being made somewhere in the app flow. Request template looks like this:

    "reqTemp": {
        "schema": {
            "method": "POST",
            "protocol": "https",
            "host": "{{some-host}}",
            "path": "{{some-path}}",
            "headers": {
               "Authorization": "Bearer <%= access_token %>",
                "Content-Type": "application/json"
              }
        },
        "options": {
            "isOAuth": true
        }        
    }
}

I try invoke it like so:

client.request.invokeTemplate("reqTemp", {
        body: JSON.stringify(payload)
      }).then(function(data) {
        // Do something with the data
      });

My config/oauth_config.json looks like this:

{
  "client_id": "xxxxxxxxxxx",
  "client_secret": "xxxxxxxxxxx",
  "authorize_url": "https://{{azure-authorize-api}}",
  "token_url": "https://{{our-internal-token-api}}",
  "token_type": "agent",
  "options": {
    "scope": "openid"
  }
}

Hi @Raviraj ,

Any chance you picked up something going wrong somewhere here?

Hey @collin,

I see that you are referring to OAuth credentials available in the browser’s localstore, are you testing the app locally by running fdk run or is the app packed and deployed as the custom app where it’s erroring up?

Can you share your oauth_config.json file with removing the values for client_id and client_secret?

Check out the OAuth docs and also sample app with OAuth.

Hi @zach_jones_noel ,

Yes I am testing the custom app locally using fdk run

My config/oauth_config.json looks like this:

{
  "client_id": "xxxxxxxxxxx",
  "client_secret": "xxxxxxxxxxx",
  "authorize_url": "https://{{azure-authorize-api}}",
  "token_url": "https://{{our-internal-token-api}}",
  "token_type": "agent",
  "options": {
    "scope": "openid"
  }
}

Hey @collin,

When the run the app fdk run, this would create a .fdk directory in your local app directory which would contain localstore and the JSON would have access_token in there. Do you see that? And still, the value is not being substituted?

Hi @zach_jones_noel ,
Yes I see the .fdk folder, I see the localstore JSON but it only has two properties: xxxx_oauth_iparams and xxxx_oauth_appstate. There is no access_token in there, however, didn’t i read here (last statement on the page literally) that if the token_type is agent (agent level authentication) then the access_token would be persisted in browser’s local storage?

This was the basis for my query because I can see the access_token in browser’s local storage for this particular custom app running on my local as shown in the except below.
Screenshot 2023-06-01 at 14.10.45

Hey @collin,
Thanks for pointing that out, I missed token_type is set to agent.

@Developer-Platform please help here with this.

Hey @collin,

Was this app functioning as expected with tokens when it was published or developed with platform 2.2 / FDK 8?

Hi @zach_jones_noel

This app is being implemented against the latest fdk version 9 as development started just about a month ago. It has not been implemented on fdk 8 as it is a new app being developed from scratch.

1 Like

Thanks for the info @collin, our team is looking into this.

CC @Nadeem_Bhati

Hi @zach_jones_noel @Raviraj

Did you find something that’s causing this issue please? Unfortunately we are at a critical stage with the project requiring this integration so we take decision whether custom app will work for our requirements or not.

If you didn’t find something yet, I don’t know if this can help in the investigations but would token length be suspect of interest causing this error as I mentioned in the original post because ours is quite lengthy??

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.