如何在ajax调用中传递访问令牌

我今天早些时候提出了一个问题 ,即使用ZenDesk API进行ajax调用。

我可以在命令提示符下成功运行以下命令并在ZenDesk中创建一个票证( my_api_token是API令牌字符串):

 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/

基于表单的网站身份validation的权威指南

Grafana在url中传递访问令牌

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

我应该做些什么不同的事情?

代码确定,但您需要使用以下URL生成api密钥。

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

在上面的URL上给出了api URL的地址并授予访问权限。 它会对你有用。

接受的答案就是我所需要的,但是我失去了一个很大的区别。 如果其他人读到这个,我认为它可能有所帮助。

当我真正需要的是api 密钥时,我试图使用api令牌。