I asked a question earlier today关于使用 ZenDesk API 进行 ajax 调用。
我可以在命令提示符下成功运行以下命令并在 ZenDesk 中创建票证(my_api_token
是 API token 字符串):
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 kawnah@email.com/token:my_api_token -X POST
我现在想了解的是如何将其转化为 ajax 调用。这是我正在关注的文档。
我现在正在做的事情如下:
$.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);
}
});
这里有一些我已经看过的答案和文档,但我仍然很困惑:
(最佳):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
我应该怎么做?
最佳答案
代码可以,但您需要使用以下 URL 生成 api key 。
https://developer.zendesk.com/requests/new
在上面的 URL 上提供您的 api URL 地址并授予访问权限。它会为你工作。
关于javascript - 如何在 ajax 调用中传递访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45448245/