403 error when creating request via API | The place for Zendesk users to come together and share
Skip to main content
January 27, 2021
Question

403 error when creating request via API

  • January 27, 2021
  • 23 replies
  • 146 views

Hi,

I'm creating a custom form for our end-users within Zendesk itself. When I try to call the https://subdomain.zendesk.com/api/v2/requests API to create the request, it returns with a 403 error. I'm using an api token for auth. The API call works in postman and I was able to create a request but when I try to call it in my custom form in zendesk, it gives me a 403 error.


We are on Proffesional.

{
"error": {
"title": "Forbidden",
"message": "Invalid authenticity token"
}
}

 

Not sure what I'm doing wrong since the call works on postman.

var myHeaders = new Headers();
myHeaders.append("Authorization", "Basic btoa(email/token:API_TOKEN)");
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Cookie", "__cfduid=d3d63f8118c012940ee1e08701ec6140d1610414533; _zendesk_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFVEkiJTBiMGNlNTVlOGVhNjQ4NTcyMDkxNGJjMzZjOWQxNTdhBjsAVEkiDGFjY291bnQGOwBGaQMvZ5JJIgpyb3V0ZQY7AEZpA7nELw%3D%3D--2608b56780c88cadb0776d6913aace910de8a12b; __cfruid=da3497d68006538ec0acea547c226758ea2a06fc-1611699971");

var raw = JSON.stringify({"request":{"subject":"TESTING API!","comment":{"body":"My printer is on fire!"}}});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'

};

fetch("https://subdomain.zendesk.com/api/v2/requests", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

23 replies

BonalizaAuthor
January 27, 2021

@Thomas Verschoren 

Yea it's giving me the correct value. I also tried encrypting the whole thing and just pasting it but it's still giving me the error message.

Could you think of anything else? I'm just lost since it's working on Postman

BonalizaAuthor
January 27, 2021

I also tried your code but it gave me the same error.

 

BonalizaAuthor
January 29, 2021

Hey, 

So I did that. It matches what I used. email/token:api_token.

Are you using this code in Zendesk or an external app?

Is it possible that the authentication is somehow clashing with the current logged in user?

BonalizaAuthor
February 1, 2021

Should the code then be different if using this within zendesk?

BonalizaAuthor
February 5, 2021

Hey @Thomas Verschoren,

I think it's an issue with the logged in user or the session...

I tried logging out of Zendesk and tried the exact code on the browser console and it worked...

Any idea why this is the case?

 

Thanks

BonalizaAuthor
February 8, 2021

Hey,

I am not making an external app. I'm doing this within our Zendesk Portal using the templates (e.g. new_request_page.hbs). Base on my understanding, ZAFClient is used when building an external app that incorporates Zendesk support? 

My issue is when doing this in Zendesk Portal itself. Also the 404 error only appears when doing a POST request.

Camila16
June 8, 2021

Bonaliza, where you able to find a solution for this? I'm having the same issue. 

December 2, 2021

Same issue here, not sure what to do...

 

Tipene
Employee
December 8, 2021

This is likely caused by a clash with how the fetch API handles cookies in the context of the Help Center, when logged in as an agent or admin. You can use the /api/v2/users endpoint to obtain an authenticity (CSRF) token which should fix the issue. Here’s an example of how that could look:
 

fetch("/api/v2/users/me")
.then((data) => {
  return res = data.json();
})
.then((res) => {
  const authToken = res.user.authenticity_token;

  let myHeaders = new Headers();
  myHeaders.append(
    "Authorization",
    "Basic btoa(email/token:API_Token)"
  );
  myHeaders.append("Content-Type", "application/json");
  myHeaders.append("X-CSRF-Token", authToken);

  const raw = JSON.stringify({
      "request": {
        "requester": { "name": "Jane Smith", "email": "jane@example.com" },
        "subject": "TESTING API!",
        "comment": { "body": "My printer is on fire!" },
      },
    });

  const requestOptions = {
    method: "POST",
    headers: myHeaders,
    body: raw,
    redirect: "follow",
  };

  fetch("/api/v2/requests", requestOptions)
    .then(response => response.text())
    .then(result => console.log(result))
    .catch(error => console.log("error", error));
});

 
I hope this helps! Feel free to reach out with any questions.
 
Tipene

Pan12
January 13, 2022

@Tipene Hughes 

I used this code, but I still does not solve my issue. 

I am using end user to login in zendesk, using email/token: token to post an API, I still get the error message:"error{title: "Forbidden", message: "Invalid authenticity token"}".

Here is my code snippet:

Can you help check whether there is something wrong? 

fetch("/api/v2/users/me")
.then((data) => {
return res = data.json();
})
.then((res) => {
const authToken = res.user.authenticity_token;
console.log(authToken);
let myHeaders = new Headers();
myHeaders.append(
"Authorization",
"Basic xxxxxxxxxxxxxxxxxxxx"
);
myHeaders.append("Content-Type", "application/json");
myHeaders.append("X-CSRF-Token", authToken);
const raw = JSON.stringify({
"organization": {
"name": "ttt"
}
});
const requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow",
};
fetch('/api/v2/organizations', requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log("error", error));
});