使用iron-ajax使用API​​令牌对freshDesk进行身份验证

时间:2016-06-14 15:32:28

标签: ajax authentication polymer

documentation here使用curl在所有示例中对其进行身份验证。各种语言也有some examples here。但是,我正在尝试将API与iron-ajax元素一起使用,但我无法通过身份验证。

我尝试将它从nodejs样本中删除并将其包含在params中,但它仍然无效。

<template is="dom-bind" id="app">
    <iron-ajax auto
               url="https://<mydomain>.freshdesk.com/api/v2/tickets"
               params='{ "user": "<myapitoken>", "pass": "X", "sendImmediately": "true" }'
               handle-as="json"
               last-response="{{ajaxResponse}}"></iron-ajax>

    <template is="dom-repeat" items="[[ajaxResponse.items]]">
        <mhc-ticket ticket-id="[[item.id]]"></mhc-ticket>
    </template>

</template>

我刚收到401 (Unauthorized)。我该怎么认证?

1 个答案:

答案 0 :(得分:1)

我必须将参数移到标题中,将with-credentials设置为true并强制method为&#34; GET&#34;。它现在有效:

<template is="dom-bind" id="app">
    <iron-ajax auto
               url="https://<mydomain>.freshdesk.com/api/v2/tickets"
               headers='{ "user": "<myapikey>", "pass": "X", "sendImmediately": "true" }'
               handle-as="json"
               method="GET"
               last-response="{{ajaxResponse}}"
               with-credentials></iron-ajax>

    <template is="dom-repeat" items="[[ajaxResponse.items]]">
        <mhc-ticket ticket-id="[[item.id]]"></mhc-ticket>
    </template>

</template>