From your screenshot I can understand you’re trying to use Basic Authentication with Api key to call freshdesk API. There are few mistakes in your code to be corrected. Let me point out those & also I’m sharing a sample working zip for your reference.
If you want to search contacts, the end point you’ve use is - /api/v2/contacts/autocomplete?term=[keyword]
Reference - Freshdesk
Note: The end point you’re trying to use works different - it’s used to get a list of contacts matching the specified contact fields specified in the query. More details - Freshdesk
In the header, the value for “Authorization” must be “Basic APIkey:X” where APIkey:X must be Base64-
encoded. Please use this tool to encode it: https://www.base64encode.org/
E.g. if your API key is WestY134, then you must encode WestY134:X to
base64, resulting in something like: “OUpKSGg6WA==”. Then you can pass the same in header
Note: Instead you can chose to get from iparams like <%= iparam.key %>
Attaching the working zip also for your reference.
Note: Make sure to replace your encoded api key & hardcoded searchKey accordingly.
Also try running the zip shared above & understand the flow. You should be able to replicate it for your usecase. As you’ve not shared your actual code snippets, I not able to predict your other parts of the application.
I did the same as you gave instructions to me. However, I am getting the same thing. Below is the screenshot. Please give me a solution and also send me your number if possible.
Did you try running this zip with your api key? Is it working fine?
I guess you’re executing the script before app initialized & activated. Where is your app.js script included? Make sure this script block is executed after that or better move this logic to app.js itself.
Screenshots doesn’t seem to help much, most of your code blocks are hidden. Please try the above zip or share us the app zip to help better.
On checking your zip, I can see still the way you encoded your API key is not as I mentioned above. I guess, if you manage to unzip & check the demo app shared above things should be more clear to you. Anyway, I’m sharing the detail below hiding the sensitive information (please make sure to replace it with actual value accordingly)
This is your current code,
var search = $('#searchKey').val();
var headers = {
"Authorization": "encoded_key",
"Content-Type": "application/json"
};
But, what is expected is
const searchKey = 'Andy from google' // hardcoded, please replace this with your actual DOM value
const headers = {
// In the header, the value for “Authorization” must be “Basic APIkey:X” where APIkey:X must be Base64-encoded.
// E.g. if your API key is W25uOtAmhj8V8zT9JJHh, then you must encode W25uOtAmhj8V8zT9JJHh:X to base64, resulting in: “VzI1dU90QW1oajhWOHpUOUpKSGg6WA==”. Then you pass in the header:
// Authorization: Basic VzI1dU90QW1oajhWOHpUOUpKSGg6WA==
// You can use this tool to encode it: https://www.base64encode.org/
"Authorization": 'Basic <Your Encoded key here>', // your encoded api key here
"Content-Type": "application/json",
}
Again requesting you to extract the sample zip shared above (or) look at our sample apps bucket to make this easier.