使用Apps脚本触发Zendesk Webhook – 无法进行身份验证

huangapple go评论73阅读模式
英文:

Triggering Zendesk webhook with Apps Script - can't authenticate

问题

I can provide a translation of the code part you provided:

我正在尝试使用Google Apps Script调用Zendesk的webhook来创建一个工单最终使用Google表单触发webhook)。Zendesk的Webhook页面有[几种不同的身份验证方法][1]我正在使用基本身份验证我无法弄清楚如何正确验证webhook我尝试了几种不同的代码变化但一直收到`"error":"Couldn't authenticate you"`的响应

在Zendesk控制台中我有一个活动的webhook请求方法设置为`post`请求格式为`JSON`身份验证方法设置为基本身份验证并将`https://myDevelopmentDomain.zendesk.com/api/v2/tickets.json`设置为终端URL

我在这里做错了什么

var ENDPOINT_URL = "https://myDevelopmentDomain.zendesk.com/api/v2/tickets.json";
function testhook() {
  var options = {
    'method': "post",
    'contentType': "application/json",
    'headers': {
      "authentication":{
        "type":"basic_auth",
        "data":{
            "username":"zendeskWebhookUsername",
            "password":"zendeskWebhookPassword"
        },
        "add_position":"header"
    }
    },
    'payload': JSON.stringify({
      "ticket": {
        "id": 35436,
        "priority": "high",
        "status": "open",
        "subject": "Help, my printer is on fire!",
        "description": "The fire is very colorful.",
        "tags": [
          "enterprise",
          "other_tag"
        ]
      }
    }),
  'muteHttpExceptions': false,
  }
  UrlFetchApp.fetch(ENDPOINT_URL, options);
}
[1]: https://developer.zendesk.com/documentation/webhooks/webhook-security-and-authentication/

Please note that the translation above includes the code you provided without any additional content.

英文:

I'm trying to call a Zendesk webhook to create a ticket with Google Apps Script (evenetually using a Google form to trigger the webhook). Zendesk's Webhook page has a few different types of auth methods and I'm using basic auth. I can't figure out how to properly authenticate the webhook. I've tried several different variations on this code but keep getting "error":"Couldn't authenticate you" as a response.

In the Zendesk console, I have an active webhook with post set at the request method, JSON as the request format, basic auth set as the authentication method, and https://myDevelopmentDomain.zendesk.com/api/v2/tickets.json set as the endpoint URL.

What am I doing wrong here?

var ENDPOINT_URL = "https://myDevelopmentDomain.zendesk.com/api/v2/tickets.json"
function testhook() {
  var options = {
    'method': "post",
    'contentType': "application/json",
    'headers': {
      "authentication":{
        "type":"basic_auth",
        "data":{
            "username":"zendeskWebhookUsername",
            "password":"zendeskWebhookPassword"
        },
        "add_position":"header"
    }
    },
    'payload': JSON.stringify({
      "ticket": {
        "id": 35436,
        "priority": "high",
        "status": "open",
        "subject": "Help, my printer is on fire!",
        "description": "The fire is very colorful.",
        "tags": [
          "enterprise",
          "other_tag"
        ]
      }
    }),
  'muteHttpExceptions': false,
  }
UrlFetchApp.fetch(ENDPOINT_URL, options);
}

答案1

得分: 1

以下是您要翻译的内容:

当我看到官方文件时,似乎以下值用于创建Webhook。 参考链接

{
  "authentication": {
    "type": "basic_auth",
    "data": {
      "username": "zendeskWebhookUsername",
      "password": "zendeskWebhookPassword"
    },
    "add_position": "header"
  }
}

我认为这可能是您当前出现“error”:“Couldn't authenticate you”的问题的原因。根据URL和“payload”的值,我猜测在您当前的情况下,您已经创建了您的Webhook,并且您可能希望使用Google Apps Script请求Webhook。

如果我的理解是正确的,以下是如何修改的建议:

修改后的脚本:

function testhook() {
  var ENDPOINT_URL = "https://myDevelopmentDomain.zendesk.com/api/v2/tickets.json";
  var options = {
    'method': "post",
    'contentType': "application/json",
    'headers': { "Authorization": "Basic " + Utilities.base64Encode("zendeskWebhookUsername:zendeskWebhookPassword") },
    'payload': JSON.stringify({
      "ticket": {
        "id": 35436,
        "priority": "high",
        "status": "open",
        "subject": "Help, my printer is on fire!",
        "description": "The fire is very colorful.",
        "tags": [
          "enterprise",
          "other_tag"
        ]
      }
    }),
    'muteHttpExceptions': false,
  }
  UrlFetchApp.fetch(ENDPOINT_URL, options);
}

注意:

  • 不幸的是,我无法测试此请求。因此,当发生错误时,请再次确认错误消息和/或您的请求体。
  • 在此修改中,假定您的Webhook已经能够使用基本授权。请注意这一点。

参考链接:

英文:

When I saw the official document, it seems that the following value is used for creating a Webhook. Ref

{
  "authentication": {
    "type": "basic_auth",
    "data": {
      "username": "zendeskWebhookUsername",
      "password": "zendeskWebhookPassword"
    },
    "add_position": "header"
  }
}

I thought that this might be the reason for your current issue of "error":"Couldn't authenticate you". From the URL and the value of payload, I guessed that in your current situation, you have already created your Webhook and you might want to request the Webhook using Google Apps Script.

If my understanding is correct, how about the following modification?

Modified script:

function testhook() {
  var ENDPOINT_URL = "https://myDevelopmentDomain.zendesk.com/api/v2/tickets.json";
  var options = {
    'method': "post",
    'contentType': "application/json",
    'headers': { "Authorization": "Basic " + Utilities.base64Encode("zendeskWebhookUsername:zendeskWebhookPassword") },
    'payload': JSON.stringify({
      "ticket": {
        "id": 35436,
        "priority": "high",
        "status": "open",
        "subject": "Help, my printer is on fire!",
        "description": "The fire is very colorful.",
        "tags": [
          "enterprise",
          "other_tag"
        ]
      }
    }),
    'muteHttpExceptions': false,
  }
  UrlFetchApp.fetch(ENDPOINT_URL, options);
}

Note:

  • Unfortunately, I cannot test this request. So, when an error occurs, please confirm the error message and/or your request body again.
  • In this modification, it supposes that your Webhook has already been able to be used with the basic authorization. Please be careful about this.

Reference:

huangapple
  • 本文由 发表于 2023年6月9日 04:21:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定