Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
110
rated 0 times [  111] [ 1]  / answers: 1 / hits: 6993  / 7 Years ago, tue, august 1, 2017, 12:00:00

I asked a question earlier today about making an ajax call with the ZenDesk API.



I can successfully run the following in command prompt and create a ticket in ZenDesk (my_api_token is the API token string):



curl https://mydomain.zendesk.com/api/v2/tickets.json 
-d '{ticket: {subject: Test ticket from API, comment: { body: Just a test -kawnah }}}'
-H Content-Type: application/json -u [email protected]/token:my_api_token -X POST


What I'm trying to understand now is how this translates into an ajax call. This is the documentation I'm following.



What I am doing at the moment is below:



$.ajax({
type: 'post',
url: 'https://domain.zendesk.com/api/v2/tickets.json',
data: {
ticket: {
subject: new contact from + $('#contactFormEmail').val(),
comment: {
body: $('#contactFormMessage').val()
}
}
},
// ==========================================================
// This is where I'm confused....
// How do I authorize the API via the token? It's here right?
// I'm trying it via standard setRequestHeader Authorization
// For learning purposes in my local copy I'm pasting the key
// straight in, just to get it working.
// I know this is bad practice.
// ==========================================================
beforeSend : function( xhr ) {
xhr.setRequestHeader( 'Authorization', 'BEARER my_api_token' );
},
success: function( response ) {
console.log(response);
},
error : function(error) {
console.log(error);
}
});


Here are some answers and docs I've looked at already but I'm still really confused:



(best one): https://auth0.com/blog/using-json-web-tokens-as-api-keys/



The definitive guide to form-based website authentication



Grafana passing access token in url



https://developer.zendesk.com/rest_api/docs/core/tickets#create-ticket



What should I be doing differently?


More From » jquery

 Answers
19

Code ok, but you need to generate api key using following URL.



https://developer.zendesk.com/requests/new



On above URL give address of your api URL and grant access. It will work for you.


[#18924] Monday, July 31, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
trentb

Total Points: 261
Total Questions: 101
Total Answers: 90

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
;