使用 google apps 脚本生成 zoho ticket

Generating zoho ticket using google apps script

我正在尝试在 google 应用程序脚本中使用 zoho desk api。

我正在尝试通过 google script.But 生成票证,但出现错误。 而如果我在 PHP 中这样做,它工作正常。

请找到两个代码以供参考:

PHP 有效的代码

            $auth_token = '12345ab';//your_auth_token
            $org_id=12345; //your_organization_id

            $ticket_data=array(
                "departmentId"=>$getdepartmentid,
                "contactId"=>$getcontactid,
                "subject"=>$ticket_subject,
                "description"=>$ticket_desc,
                "priority"=>$priority,
                "status"=>$ticketstatus,
                "email"=>$contact_email,
                "classification"=>$classification,
                "channel"=>"Application"
            );

        $headers=array(
            "Authorization: $auth_token",
            "orgId: $org_id",
            "contentType: application/json; charset=utf-8",
        );

        $url="https://desk.zoho.in/api/v1/tickets";

        $ticket_data=(gettype($ticket_data)==="array")? json_encode($ticket_data):$ticket_data;

        $ch= curl_init($url);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
        curl_setopt($ch,CURLOPT_POST,TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$ticket_data); //convert ticket data array to json
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    


        $response= curl_exec($ch);
        $info= curl_getinfo($ch);

GOOGLE APPS 脚本(不工作)

        var authtoken = "12345ab"; //your_auth_token
        var org_id=12345; //your_organization_id
        var department=23220000000057620; // ID of department
        var contact=23220000000066959; //ID of customer
        var subject=location_urgent_inbox_folder_name + ' /' + Name_of_file_to_be_attached;
        var description="Ticked via drive";
        var status="open";
        const ticketData = {
         subject: subject, 
         departmentId: department, // Replace this with whatever yours is.
         contactId: contact, // Replace this with whatever yours is.
         description: description,
         status: status
        };
        const zohoUrl = 'https://desk.zoho.in/api/v1/tickets';
        try {
          var response = UrlFetchApp.fetch(zohoUrl, {
          "method": 'POST',
          "muteHttpExceptions": true,
           "headers": {
           Authorization: authtoken,
          orgId: org_id,
          contentType: 'application/json',
         },
        "payload": JSON.stringify(ticketData),
        });
        Logger.log(response.getContentText());
        const parsed = JSON.parse(response.getContentText());
        } catch (error) {
          Logger.log(error.toString());
        }             

假设我正在查看正确文档的正确部分 (https://desk.zoho.com/DeskAPIDocument#Tickets_Createaticket),我认为这些是您的问题:

  1. 服务器的 JSON-encoded 响应不包含 message 属性,因此 Logger.log(result.message); 记录 undefined。 (也许尝试记录 response.getContentText() 以查看您的情况下可用的属性——或参考 API 文档。)
  2. 您发送的请求中缺少
  3. AuthorizationorgId headers。 (看起来 authtokens 已被弃用 (https://desk.zoho.com/DeskAPIDocument#Authentication) and you instead need to use OAuth 2.0 (https://desk.zoho.com/DeskAPIDocument#OauthTokens)。)
  4. 需要在请求的 body 中发送数据。 (您似乎是在查询字符串中发送它。)
  5. 我没有详细阅读文档,但我没有看到任何关于查询字符串参数 authtokenJSONString 的提及(CTRL+F returns 不匹配)。因此,您可能希望在代码中摆脱它们,而是按照文档中的说明进行操作。

下面的代码未经测试,无法使用(因为您需要用自己的凭据替换)。但它应该让您了解如何完成此任务。

// See: https://desk.zoho.com/DeskAPIDocument#Tickets#Tickets_Createaticket
// Required: subject, departmentId, contactId
const ticketData = {
    subject: location_urgent_inbox_folder_name + ‘ /’ + Name_of_file_to_be_attached, // Taken from your question. Presume these are declared, valid and in scope.
    departmentId: '12345', // Replace this with whatever yours is.
    contactId: '12345', // Replace this with whatever yours is.
    description: 'Ticked via drive',
    status: 'open'
};

const zohoUrl = 'https://desk.zoho.in/api/v1/tickets';

try {
    // See: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl,-params
    const response = UrlFetchApp.fetch(zohoUrl, {
        method: 'POST',
        contentType: 'application/json',
        headers: {
            orgId: '12345' // Replace this with whatever yours is.
            Authorization: 'Zoho-oauthtoken ....' // See: https://desk.zoho.com/DeskAPIDocument#OauthTokens
        },
        payload: JSON.stringify(ticketData),
    });
    Logger.log(response.getContentText());
    const parsed = JSON.parse(response.getContentText());
} catch (error) {
   Logger.log(error.toString());
}

我从未使用过任何 Zoho 的 APIs,也有一段时间没有使用过 Google Apps Script,如果我遗漏了什么,请深表歉意。

问题已解决, 发布答案以便对某人有所帮助

         //create ticket in zoho

        var authtoken = "abcd"; //your_auth_token
        var org_id="12345"; //your_organization_id
        var department="54321"; // ID of department in which ticket to be raised
        var contact="9999"; //ID of customer
        var subject="Ticket via google script";
        var description="Ticket via google script";
        var status="open";
        const ticketData = {
           subject: subject, 
           departmentId: department, // Replace this with whatever yours is.
           contactId: contact, // Replace this with whatever yours is.
           description: description,
           status: status
        };
        const zohoUrl = 'https://desk.zoho.in/api/v1/tickets';
        try {
          var response = UrlFetchApp.fetch(zohoUrl, {
          "method": 'POST',
          "muteHttpExceptions": true,
          "headers": {
           Authorization: authtoken,
           orgId: org_id,
           contentType: 'application/json',
         },
         "payload": JSON.stringify(ticketData),
         });
         const parsed = JSON.parse(response.getContentText());
         } catch (error) {
          Logger.log(error.toString());
         }