如何在ajax调用中传递访问令牌 [英] How to pass an access token in an ajax call

查看:159
本文介绍了如何在ajax调用中传递访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天早些时候提出一个问题关于拨打ajax电话使用ZenDesk API。

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

我可以在命令提示符下成功运行以下命令并在ZenDesk中创建一个票证( my_api_token 是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 kawnah@email.com/token:my_api_token -X POST

我现在想要了解的是如何将其转换为ajax调用。这是我正在关注的文档。

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:

(最好的一个): https://auth0.com/blog/using-json-web-tokens-as-api-keys/

< a href =https://stackoverflow.com/questions/549/the-definitive-guide-to-form-b​​ased-website-authentication/477578#477578>基于表单的网站身份验证的权威指南

Grafana传递访问令牌网址

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

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

What should I be doing differently?

推荐答案

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

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

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

在上面的URL上提供api URL的地址并授予访问权限。它适用于你。

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

这篇关于如何在ajax调用中传递访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆