简体   繁体   English

ZENDESK API 客户端与 HTTP 请求/响应

[英]ZENDESK API Clients vs HTTP Requests/Responses

I'm working on a new web application in Node JS that needs to access Zendesk to check if there are tickets created for a specific user/requester e-mail (not agent e-mail).我正在 Node JS 中开发一个新的 web 应用程序,该应用程序需要访问 Zendesk 以检查是否为特定用户/请求者电子邮件(而不是代理电子邮件)创建了票证。

I came accross two scenarios where I could use to make this request.我遇到了两个可以用来提出这个请求的场景。 The first one is recommended by Zendesk, which is access Zendesk using one of two API Clients available for Node JS.第一个是 Zendesk 推荐的,它是使用 Node JS 可用的两个 API 客户端之一访问 Zendesk。 They are node-zendesk by Farrin Reid and Zendesk NodeJS API written by Adam Gray.它们是 Farrin Reid 的node-zendesk和 Adam Gray 编写的Zendesk NodeJS API Check it out here 在这里查看

Another way is to make simple HTTP Request/Responses as usual in Node, which in my case fits better for what I need.另一种方法是在 Node 中像往常一样制作简单的 HTTP 请求/响应,这在我的情况下更适合我的需要。 I just need to retrieve only that information and don't want more dependencies in my app for things I woudn't need.我只需要检索这些信息,并且不希望我的应用程序对我不需要的东西有更多的依赖。

The problem is that I am getting the message "Couldn't authenticate you" from Zendesk when I do the fetch.问题是当我进行提取时,我从 Zendesk 收到消息“无法验证您”。 Maybe someone already came accross this problem and could help me out clarifying where exactly is my mistake or what I'm missing in the code.也许有人已经遇到过这个问题,可以帮助我澄清我的错误到底在哪里,或者我在代码中遗漏了什么。

I would appreciate a light on that.我希望能对此有所了解。

Here is my code:这是我的代码:

   async function getZendeskTickets(clientEmail) {

    const zendeskAgent = 'agent@email.com'
    const api_token = 'some token'
    const query = `type:ticket requester:${clientEmail}`;
    const url = `https://mydomain.zendesk.com/api/v2/search.json?query=${query}`

    try {
        const res = await fetch(url, {
            method: 'GET',
            mode: 'no-cors',
            headers: {
                'Content-Type': 'application/json',
                Authorization: Buffer.from(`${zendeskAgent}/token:${api_token}`).toString('base64'),
            }
        })
        let data = await res.json();
        console.log('response', JSON.parse(JSON.stringify(data)))
    } catch (error) {
        console.log('zendesk response error', error)
    }

    return
} 

export default getZendeskTickets;导出默认 getZendeskTickets;

I found out what was causing the problem.我发现了导致问题的原因。

According to the Zendesk Docs, the Authorization in the header must be base64 enconded.根据 Zendesk Docs,header 中的授权必须是 base64 enconded。 I was doing in the wrong way.我做错了。 Also.还。 it must heve the word "Basic" before the credential.它必须在证书前加上“基本”一词。

While the encode can be done using buffer, I just access the site https://www.base64encode.org/ to encode the credential zendeskAgent/token:api_token.虽然可以使用缓冲区完成编码,但我只需访问站点https://www.base64encode.org/即可对凭证 zendeskAgent/token:api_token 进行编码。 The final code would be like:最终代码如下:

    const res = await fetch(url, {
    method: 'GET',
    mode: 'no-cors',
    headers: {
        'Content-Type': 'application/json',
        Authorization: 'Basic YWdlbnRAZW1haWwuY29tOnNvbWUgdG9rZW4='

} }) } })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM