'Fetch' command -> 401 Un-authorized Error Code

Good Day,
I am coming to post here since Support asked me to do so!

My Javascript below should normally allow me to call the API, but for some reason, the server keeps rejecting me ( 401 Un-authorized Error Code)

This is my Javascript Code

[
fetch(“https://mydoamin.freshdesk.com/api/v2/tickets?key=myapikey”, {
method: ‘GET’,
headers: {
‘Content-Type’: ‘application/json’
}
})
.then(function(response) {
return response.json();
})
.then(function(json) {
return json;
});
]
As an alternative, I developped this code :slight_smile:

[
fetch(‘https://mydomain.freshdesk.com/api/v2/tickets’, {
method: ‘GET’,
headers: {
‘Content-Type’: ‘application/json’,
‘Authentication’: ‘myAPIkey’
}
})
.then(function(response) {
return response.json();
})
.then(function(json) {
return json;
});
]

but this is what I receive from the server:

Could you help me finding the root cause of my authentication issue, please?

Thank you very much indeed!

Rock E. Feller

Hi @Rock.E.Feller ,

The Freshdesk API (all Freshworks product API’s) expect the API Key to be encoded and to be passed in headers. Kindly apply change your headers as shown below

    headers: {
      Authorization: "Basic <%= encode(your_api_key) %>",
      "Content-Type": "application/json",
    }

This should resolve it. Further, I recommend using iparams to securely access sensitive information such as API key.

Regards,
Thakur

Good Day Thakur

Thanks for your response, so I tried again but yet I am still getting the same error messge:

‘Failed to load resource: the server responded with a status of 401 (Unauthorized)’

This is my piece of code:

fetch(‘https://mydomain.freshdesk.com/api/v2/tickets’, {
method: ‘GET’,
headers: {
Authorization: “Basic <%= encode(mYApIKEy) %>”,
‘Content-Type’: ‘application/json’
}
})
.then(resp => resp.json())
.then(json => console.log(json))

image

Is there anything I missed here? Can you advise, please?

Thanks alot !!!

R

Good Day everybody…is here anyone who could assist regarding my Authentication issue please?
401 Error Code / Authentication issue with fetch + api key
Cheers :wink:

Hi @Rock.E.Feller ,

Could you please confirm if you are using the right FDK version and credentials? If so kindly share the app and account details as personal message. I shall take a look at the logs to see if theres anything wrong.

Regards,
Thakur

Hi @Rock.E.Feller ,

Could you please confirm if this issue is still pertinent or shall I mark this topic as closed for now?

Regards,
Thakur