Error: 'JWT strings must contain exactly 2 period characters. Found: 1

Hi!

I am new to custom app development and am trying to make a Freshchat API call during a product event in a serverless app. I have substituted the API key as seen below but the code does not work with the following error being displayed when I test it in the local environment. Can you let me know how I could include the API key present in my iparam_test_data.json file in my server.js file?

var get_url = "https://api.freshchat.com/v2/users/"+user_id;
    var req_get = {
      url: get_url,
      json: true,

      headers: {
        "Content-Type": "application/JSON",
        "Authorization": "Bearer <%= iparam.apiKey %>"
      }
    };

    request(req_get, function(error, response){
      handler.handleResponse(error, response);
      console.log(response);
      
      var finalResponse = response;

      console.log("Current last name is "+finalResponse.body.last_name);

I get the following error:
image

1 Like

Hi @Leah_James,

Typically this error implies that the entire Freshchat API key has not been copied properly.

Now I believe you are already doing this but I assume you are using your browser’s auto-complete to fill the token in the Installation’s page due to which you are getting this error. Try copying the API key from Settings–> API Tokens and then paste it. This should resolve the error.

Sidenote, the newer version of the FDK does not support the iparam_test_data.json file anymore. Instead, when running the app locally, you can set up your configs from the localhost link.

3 Likes

Hi @Arjun_Paliath

Thank you for your reply.

When I manually paste the API token in the localhost link, I get the following error. Could you let me know what I’m doing wrong?

Hey @Leah_James,

Can you confirm if you are using a Custom Installation page for your marketplace app? If so, please note that the SDK will look for these three mandatory functions in the iparams.html file.

  1. postConfigs() - This is to store the data when the marketplace app is being installed.
  2. getConfigs() - This is to retrieve the stored data from the marketplace. Typically used when editing the installation’s page after the app has been installed.
  3. validate() - This is used to validate the custom installations page.

The documentation for the same is available in the link here.

Hope this helps.

1 Like

Hi @Arjun_Paliath,

No, I’m not using a custom installation page. The app only requires the API token so the iparams.json file contains the following code.
image

Thanks

@Leah_James - welcome on to freshworks developer community :tada:

I am trying to understand what app is intending to achieve here. As far as I understood, app is trying to make an API call as soon as an product event is happened. This API call is made from the server.js in node environment.

One good way to achieve it would be,

  1. Use iparam.json and have app get API key from the configuration page.
  2. Once you have the API key, mark it secure.
  3. Generally the formation of using <% = iparam.apiKey %> is helpful if app makes API calls from frontend. See this detailed answer.
  4. But in your described situation, API calls are made from server.js on a product event trigger. Look for the payload that your function would receive as an argument to the parameter. It would be something like this…
{
  "account_id" : "value",
  "event"  : "value",
  "region"  : "value",
  "timestamp"  : "value",
  "data" : {
    //Contains the list of objects related to the event.
  },
  "iparams" : {
    "Param1" : "value",
    "Param2" : "value"
  }
}

Where Param1 and Param2 would those described in app’s config/iparams.json
5. It also means your API key would be accessible by payload.iparams.apikey. App can use to make API calls from the backend.

:arrow_heading_up: Do let us know if that helped you make some progress…

However,

  1. May I know how you learnt to use iparam_test_data.json ? This is deprecated and no longer expected to be used.
  2. Could let me know where are you seeing the error as present in screenshot? Is it on Freshworks CLI (called FDK) ?
2 Likes

Hi @Saif

Thank you for your detailed answer.

I am now adding the API key as seen below using the payload args.iparams.apiKey.
image

In this case, if I add the API key directly in iparams.json like the following, I am getting an error in the terminal.
{ "apiKey":"Value" }

Error:
The local server could not be started due to the following issue(s):
[ERROR] Exception occured while validation: Cannot use 'in' operator to search for 'options' in eyJhbGciOiJSUzI1NiIsInR5cCI.........." 

I also tried pasting the Bearer Token in the custom installation local link though I don’t have custom installation parameters(only API token) and the error I receive is the previous “Post config…” along with the “JWT strings must contain exactly 2 period characters. Found: 0”

Can you let me know what is the right way to test if the API token is accessible from iparams.json?

Answers to your questions:

  1. May I know how you learnt to use iparam_test_data.json ? This is deprecated and no longer expected to be used.
  • The iparam_test_data.json file was available under the config folder when I created the app so I figured maybe it could be used but I understand now that it can’t.
  1. Could let me know where are you seeing the error as present in screenshot? Is it on Freshworks CLI (called FDK) ?
  • I logged the response of the API call and that is where the error was displayed in the body in my terminal.
1 Like

Let’s try few more things to sort this issue,

  1. I see you are using request npm package. Did you list them in the manifest.json? See documentation.
  2. When you run the CLI by $ fdk run , can you console.log(args) and share a snippet on this thread. Just trying to see if there is a problem in how we are accessing the apikey.
  3. Do you by any chance have iparams.html in your config folder? Because only either one of configuration page is support. [iparams.json or iparam.html] You cannot use both at once. I am assuming you are using iparam.json. A screenshot of app directory can help me confirm it.
  4. You can further get additional logs by running $NODE_DEBUG=FDK fdk run Can you run that command and let share the logs on this thread?
1 Like

Thank you, Saif.

The answers to your questions:

  1. I see you are using request npm package. Did you [list them in the manifest.json]…
  • Yes.
  "platform-version": "2.0",
  "product": {
    "freshchat": {}
  },
  "dependencies": {
    "request": "2.85.0",
    "base-64": "0.1.0",
    "moment":"2.22.2"
}
}
  1. When you run the CLI by $ fdk run , can you console.log(args) and share a snippet on this thread. Just trying to see if there is a problem in how we are accessing the apikey.
  • Sure
{ data:
       { conversation:
      { user_id: '678a7a77-de58-4f7a-a707-fd8d0c7699ad',
        reopened_time: '2019-07-29T14:51:56.933Z',
        assigned_time: '2019-07-29T14:55:00.234Z',
        created_time: '2019-07-25T10:01:23.564Z',
        response_due_type: 'NO_RESPONSE_DUE',
        conversation_id: '509040f3-e601-4c82-82a2-6e5dcc813d6e',
        app_id: '032c91b6-4b46-4f5b-adcb-102c72cd4efa',
        messages: [Array],
        status: 'new',
        channel_id: 'd2f99658-ff98-44b8-b5c1-3a19dbfe2a2d',
        assigned_agent_id: '2823233e-7122-4f87-9d56-38aaaaeb676a',
        assigned_group_id: '40753ac3-1f66-40d9-b903-23db0a9a70b0' },
     associations:
      { group: [Object],
        channel: [Object],
        user: [Object],
        agent: [Object] },
     actor:
      { type: 'system',
        sub_type: 'user',
        id: '2823233e-7122-4f87-9d56-38aaaaeb676a',
        first_name: 'Jon',
        last_name: 'Doe',
        email: 'jon.doe@freshworks.com',
        phone: '9876543212',
        avatar: [Object] },
     changes: { model_changes: [Object] } },
      region: 'US',
      account_id: '438346485531979',
      domain: 'example.freshchat.com',
      event: 'onConversationUpdate',
      timestamp: 1564394238524,
      version: '1.0.0',
      iparams: {} } 

Content of iparams.json:

image

Continuation…
3. Do you by any chance have iparams.html in your config folder? Because only either one of configuration page is support. [iparams.json or iparam.html] You cannot use both at once. I am assuming you are using iparam.json. A screenshot of app directory can help me confirm it.

  • Sure.
    image
  1. You can further get additional logs by running $NODE_DEBUG=FDK fdk run Can you run that command and let share the logs on this thread?
  • Sure.
        To simulate product, app setup, and external events, visit - http://localhost:10001/web/test
    To test the installation page, visit - http://localhost:10001/custom_configs
    FDK 4410: (data-util.js) Read {"update_101_101_dpn_hash":"2e2b54dc196c7e58212ea49aa6ac2b57"}
    FDK 4410: (beevents.js) Using {} as iparams for testing.
    FDK 4410: (beevents.js) Calling event handler with {"categoryName":"productEvent","categoryArgs":{"methodName":"onConversationUpdate","methodParams":{"data":{"conversation":{"user_id":"678a7a77-de58-4f7a-a707-fd8d0c7699ad","reopened_time":"2019-07-29T14:51:56.933Z","assigned_time":"2019-07-29T14:55:00.234Z","created_time":"2019-07-25T10:01:23.564Z","response_due_type":"NO_RESPONSE_DUE","conversation_id":"509040f3-e601-4c82-82a2-6e5dcc813d6e","app_id":"032c91b6-4b46-4f5b-adcb-102c72cd4efa","messages":[{"message_parts":[{"text":{"content":"Conversation was reopened by John Doe"}}],"app_id":"032c91b6-4b46-4f5b-adcb-102c72cd4efa","actor_id":"2823233e-7122-4f87-9d56-38aaaaeb676a","id":"f0cf8a2a-0772-4cce-be54-ebcb1bc68860","channel_id":"d2f99658-ff98-44b8-b5c1-3a19dbfe2a2d","conversation_id":"509040f3-e601-4c82-82a2-6e5dcc813d6e","actor_type":"user","created_time":"2019-07-29T15:27:17.972Z"}],"status":"new","channel_id":"d2f99658-ff98-44b8-b5c1-3a19dbfe2a2d","assigned_agent_id":"2823233e-7122-4f87-9d56-38aaaaeb676a","assigned_group_id":"40753ac3-1f66-40d9-b903-23db0a9a70b0"},"associations":{"group":{"id":"40753ac3-1f66-40d9-b903-23db0a9a70b0","name":"Support","description":"L1 support group","routing_type":"DISABLED"},"channel":{"id":"d2f99658-ff98-44b8-b5c1-3a19dbfe2a2d","icon":{},"updated_time":"2019-06-23T14:00:45.208Z","enabled":true,"public":true,"name":"Inbox","tags":[],"welcome_message":{"message_parts":[{"text":{"content":"Hello there! Need help? Reach out to us right here, and we'll get back to you as soon as we can!"}}],"message_type":"normal"},"locale":""},"user":{"created_time":"2019-07-29T00:51:50.776Z","id":"678a7a77-de58-4f7a-a707-fd8d0c7699ad","first_name":"","last_name":"","email":"rebecca@gmail.com","reference_id":"john.doe.198","phone":"9876543212","properties":[{"name":"first_name","value":"Luther"},{"name":"last_name","value":"Hargreeves"}],"avatar":{"url":"https://s3.amazonaws.com/fresh-chat-names/img/john_doe.png"}},"agent":{"groups":[],"status":0,"id":"527fe78d-d157-43fb-b86a-47aa9d3b2bdd","first_name":"Jimmy","last_name":"Manolo","email":"jimmy.manoloexample@gmail.com","avatar":{"url":"https://s3.amazonaws.com/fresh-chat-names/img/jon_snow.png"},"social_profiles":[]}},"actor":{"type":"system","sub_type":"user","id":"2823233e-7122-4f87-9d56-38aaaaeb676a","first_name":"Jon","last_name":"Doe","email":"jon.doe@freshworks.com","phone":"9876543212","avatar":{"url":"https://s3.amazonaws.com/fresh-chat-names/img/jon_doe.png"}},"changes":{"model_changes":{"assigned_agent_id":["527fe78d-d157-43fb-b86a-47aa9d3b2bdd",null],"assigned_group_id":["40753ac3-1f66-40d9-b903-23db0a9a70b0",null],"status":["resolved","new"]}}},"region":"US","account_id":"438346485531979","domain":"example.freshchat.com","event":"onConversationUpdate","timestamp":1564394238524,"version":"1.0.0","iparams":{}}}}
    FDK 4410: (coverage-util.js) Instrumenting /Users/ssenthil/Downloads/UpdateTest/server/server.js
    FDK 4410: (coverage-util.js) Instrumenting /Users/ssenthil/Downloads/UpdateTest/server/lib/handle-response.js
    { data:
       { conversation:
          { user_id: '678a7a77-de58-4f7a-a707-fd8d0c7699ad',
            reopened_time: '2019-07-29T14:51:56.933Z',
            assigned_time: '2019-07-29T14:55:00.234Z',
            created_time: '2019-07-25T10:01:23.564Z',
            response_due_type: 'NO_RESPONSE_DUE',
            conversation_id: '509040f3-e601-4c82-82a2-6e5dcc813d6e',
            app_id: '032c91b6-4b46-4f5b-adcb-102c72cd4efa',
            messages: [Array],
            status: 'new',
            channel_id: 'd2f99658-ff98-44b8-b5c1-3a19dbfe2a2d',
            assigned_agent_id: '2823233e-7122-4f87-9d56-38aaaaeb676a',
            assigned_group_id: '40753ac3-1f66-40d9-b903-23db0a9a70b0' },
         associations:
          { group: [Object],
            channel: [Object],
            user: [Object],
            agent: [Object] },
         actor:
          { type: 'system',
            sub_type: 'user',
            id: '2823233e-7122-4f87-9d56-38aaaaeb676a',
            first_name: 'Jon',
            last_name: 'Doe',
            email: 'jon.doe@freshworks.com',
            phone: '9876543212',
            avatar: [Object] },
         changes: { model_changes: [Object] } },
      region: 'US',
      account_id: '438346485531979',
      domain: 'example.freshchat.com',
      event: 'onConversationUpdate',
      timestamp: 1564394238524,
      version: '1.0.0',
      iparams: {} }
    User custom first name: Luther
    User custom last name: Hargreeves
    {"status":400,"method":"GET","path":"/v2/users/678a7a77-de58-4f7a-a707-fd8d0c7699ad","message":"Error. Please visit https://developers.freshchat.com/api and go to Getting Started->Errors to debug the error"}
    Last name is: undefined
    Custom property first_name or last_name is empty
    FDK 4410: (coverage-util.js) Writing coverage. 

@Leah_James,

So far you’ve been doing great. Broadly following is the procedure to how app can access the API key to make the api call as you are expecting,

  1. Once an ‘iparam’ in mentioned in iparams.json file, platform will render the UI automatically so you don’t have to. Once the $fdk run is running on :10001, visit localhost:10001/custom_configs (a suggestion on CLI will also apprear…)
  2. As you open the browser, you’ll see a simulation page. Something like the one you’ve discovered. But there would be a text field with display name as API Key.
  3. Enter the API key up and click ‘Install’. This simulation is similar to when user clicks on the Install btn.
  4. You will notice under .fdk/ folder API key entered is stored.
  5. In server.js I assume your code is trying to run under a product event. For example, let’s say onConversationCreate.
  6. You can simulate the onConversationCreate by opening another simulation page http://localhost:10001/web/test. Just select onConversationCreate and click on simulate
  7. That is when the API key that you stored will be substituted in args so that you can make the request.

From the information that you’ve shared,

The iparams value is an empty object. It suggests that you might not have done #1 to #3 steps. Let me know if you see any issue in between any of these steps.

Hi @Saif,

Thank you for your suggestions.

Yes, I have followed steps #1 to #3. I entered fdk run and I received the following suggestion on the CLI-

image

However, after I entered the API token, clicked on Install, the same error was displayed. I then tested the product event and still iparams was empty. I don’t have an iparams.html file in my config folder.

Video - iparams?

@Leah_James,

Good that we got #1 to #3 covered. It is little weird that not having ‘iparams.html’ is throwing the same error. We could further try to solve the problem with you sharing us two things

  1. An HAR file.
  2. The same NODE DEBUG logs as you have shared before.

Record these two files, while you try to do #2 to #3 ,

Note: This editor will allow you to upload HAR file and for the logs you can share as you did previously.

Optionally, Try out an sample app at your convenience

1 Like

Hi @Saif,

Thank you.

I have included the logs below and attached the HAR file.
`
Starting local testing server at http://*:10001/
Append ‘dev=true’ to your Freshchat account URL to start testing
e.g. https://web.freshchat.com/a/230117337757345/inbox/3/0/conversation/233020826993979?dev=true
Quit the server with Control-C.

To simulate product, app setup, and external events, visit - http://localhost:10001/web/test
To test the installation page, visit - http://localhost:10001/custom_configs
FDK 3934: (data-util.js) Read {"update_101_101_dpn_hash":"2e2b54dc196c7e58212ea49aa6ac2b57"}
FDK 3934: (data-util.js) Read {"update_101_101_dpn_hash":"2e2b54dc196c7e58212ea49aa6ac2b57"}
FDK 3934: (iframe.js) Responding to product with {"configs":{},"displayName":"UpdateTest","product":{"freshchat":{}},"features":["db","backend"],"v2":[]}
FDK 3934: (data-util.js) Read {"update_101_101_dpn_hash":"2e2b54dc196c7e58212ea49aa6ac2b57"}
FDK 3934: (iframe.js) Responding to product with {"configs":{},"displayName":"UpdateTest","product":{"freshchat":{}},"features":["db","backend"],"v2":[]}
FDK 3934: (data-util.js) Read {"update_101_101_dpn_hash":"2e2b54dc196c7e58212ea49aa6ac2b57"}
FDK 3934: (beevents.js) Using {} as iparams for testing.
FDK 3934: (beevents.js) Calling event handler with {"categoryName":"productEvent","categoryArgs":{"methodName":"onConversationUpdate","methodParams":{"data":{"conversation":{"user_id":"678a7a77-de58-4f7a-a707-fd8d0c7699ad","reopened_time":"2019-07-29T14:51:56.933Z","assigned_time":"2019-07-29T14:55:00.234Z","created_time":"2019-07-25T10:01:23.564Z","response_due_type":"NO_RESPONSE_DUE","conversation_id":"509040f3-e601-4c82-82a2-6e5dcc813d6e","app_id":"032c91b6-4b46-4f5b-adcb-102c72cd4efa","messages":[{"message_parts":[{"text":{"content":"Conversation was reopened by John Doe"}}],"app_id":"032c91b6-4b46-4f5b-adcb-102c72cd4efa","actor_id":"2823233e-7122-4f87-9d56-38aaaaeb676a","id":"f0cf8a2a-0772-4cce-be54-ebcb1bc68860","channel_id":"d2f99658-ff98-44b8-b5c1-3a19dbfe2a2d","conversation_id":"509040f3-e601-4c82-82a2-6e5dcc813d6e","actor_type":"user","created_time":"2019-07-29T15:27:17.972Z"}],"status":"new","channel_id":"d2f99658-ff98-44b8-b5c1-3a19dbfe2a2d","assigned_agent_id":"2823233e-7122-4f87-9d56-38aaaaeb676a","assigned_group_id":"40753ac3-1f66-40d9-b903-23db0a9a70b0"},"associations":{"group":{"id":"40753ac3-1f66-40d9-b903-23db0a9a70b0","name":"Support","description":"L1 support group","routing_type":"DISABLED"},"channel":{"id":"d2f99658-ff98-44b8-b5c1-3a19dbfe2a2d","icon":{},"updated_time":"2019-06-23T14:00:45.208Z","enabled":true,"public":true,"name":"Inbox","tags":[],"welcome_message":{"message_parts":[{"text":{"content":"Hello there! Need help? Reach out to us right here, and we'll get back to you as soon as we can!"}}],"message_type":"normal"},"locale":""},"user":{"created_time":"2019-07-29T00:51:50.776Z","id":"678a7a77-de58-4f7a-a707-fd8d0c7699ad","first_name":"","last_name":"","email":"rebecca@gmail.com","reference_id":"john.doe.198","phone":"9876543212","properties":[{"name":"first_name","value":"Luther"},{"name":"last_name","value":"Hargreeves"}],"avatar":{"url":"https://s3.amazonaws.com/fresh-chat-names/img/john_doe.png"}},"agent":{"groups":[],"status":0,"id":"527fe78d-d157-43fb-b86a-47aa9d3b2bdd","first_name":"Example","last_name":""Name123","email":"john@gmail.com","avatar":{"url":"https://s3.amazonaws.com/fresh-chat-names/img/jon_snow.png"},"social_profiles":[]}},"actor":{"type":"system","sub_type":"user","id":"2823233e-7122-4f87-9d56-38aaaaeb676a","first_name":"Jon","last_name":"Doe","email":"jon.doe@freshworks.com","phone":"9876543212","avatar":{"url":"https://s3.amazonaws.com/fresh-chat-names/img/jon_doe.png"}},"changes":{"model_changes":{"assigned_agent_id":["527fe78d-d157-43fb-b86a-47aa9d3b2bdd",null],"assigned_group_id":["40753ac3-1f66-40d9-b903-23db0a9a70b0",null],"status":["resolved","new"]}}},"region":"US","account_id":"438346485531979","domain":"example.freshchat.com","event":"onConversationUpdate","timestamp":1564394238524,"version":"1.0.0","iparams":{}}}}
FDK 3934: (coverage-util.js) Instrumenting /Users/ssenthil/Downloads/UpdateTest/server/server.js
FDK 3934: (coverage-util.js) Instrumenting /Users/ssenthil/Downloads/UpdateTest/server/lib/handle-response.js
{ data:
   { conversation:
      { user_id: '678a7a77-de58-4f7a-a707-fd8d0c7699ad',
        reopened_time: '2019-07-29T14:51:56.933Z',
        assigned_time: '2019-07-29T14:55:00.234Z',
        created_time: '2019-07-25T10:01:23.564Z',
        response_due_type: 'NO_RESPONSE_DUE',
        conversation_id: '509040f3-e601-4c82-82a2-6e5dcc813d6e',
        app_id: '032c91b6-4b46-4f5b-adcb-102c72cd4efa',
        messages: [Array],
        status: 'new',
        channel_id: 'd2f99658-ff98-44b8-b5c1-3a19dbfe2a2d',
        assigned_agent_id: '2823233e-7122-4f87-9d56-38aaaaeb676a',
        assigned_group_id: '40753ac3-1f66-40d9-b903-23db0a9a70b0' },
     associations:
      { group: [Object],
        channel: [Object],
        user: [Object],
        agent: [Object] },
     actor:
      { type: 'system',
        sub_type: 'user',
        id: '2823233e-7122-4f87-9d56-38aaaaeb676a',
        first_name: 'Jon',
        last_name: 'Doe',
        email: 'jon.doe@freshworks.com',
        phone: '9876543212',
        avatar: [Object] },
     changes: { model_changes: [Object] } },
  region: 'US',
  account_id: '438346485531979',
  domain: 'example.freshchat.com',
  event: 'onConversationUpdate',
  timestamp: 1564394238524,
  version: '1.0.0',
  iparams: {} }

AppTest.har (802.2 KB)

HI @Leah_James,

I have dropped a message to you on this forum inbox to gather some more information as provided info seems very much okay and not helpful to debug further.

Please check on it.

Thank you for looking into this, @Saif

I don’t know if it has already been answered but the request module does not have the templatizing support like $request. Possibly the cause of your error?

2 Likes

For the followers of thread,

  • We joined over a call to find a fix for this issue. Although, we weren’t able to reproduce this issue, we eventually updated the Freshworks CLI and had this issue resolved.
3 Likes