【问题标题】:How to pass an access token in an ajax call如何在 ajax 调用中传递访问令牌
【发布时间】:2017-08-02 21:37:15
【问题描述】:

I asked a question earlier today 关于使用 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/

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

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

【问题讨论】:

    标签: javascript jquery ajax


    【解决方案1】:

    代码没问题,但你需要使用以下 URL 生成 api 密钥。

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

    在上面的 URL 中提供您的 api URL 的地址并授予访问权限。它会为你工作。

    【讨论】:

    • 生成的代码将与 Bearer api_code 和 Authorization header 一起使用
    • 是的,这正是我所需要的!我在任何地方都找不到...你在哪里找到的?
    • 我尝试了一整天的相同解决方案,然后在技术支持页面中找到。以同样的方式解决了我的问题。我正在使用控制台应用程序的方法。那就是我提到的两端使用令牌访问 ZenDesk API 的方法是相同的
    • 在 op 中添加其他细节 :)
    【解决方案2】:

    接受的答案是我需要的,但我缺少一个很大的区别。如果其他人读到这篇文章,我认为它可能会有所帮助。

    我试图使用 api 令牌,而我真正需要的是 api key

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-29
      • 2013-06-24
      • 2018-10-23
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 2019-12-04
      相关资源
      最近更新 更多