简体   繁体   English

带有Freshdesk API错误的Google App API

[英]Google app api with freshdesk api error

On using the freshdesk api from google app script getting an error 在使用Google App脚本中的freshdesk API时遇到错误

"{"code":"invalid_content_type","message":"Content-Type header is set to . It should be set to application/json"}"

The code used for this 用于此的代码

function hd_getTickets(){//using v2
    var API_KEY = 'xxxxxxxxxxxxxx';
    var headers = {'Content-type': 'application/json','Authorization': 'Basic ' + Utilities.base64Encode(API_KEY + ':X')    };
    var data = {  "query":"\"priority:3\"" };
    var ENDPOINT = 'https://xxxxxxx.freshdesk.com/api/v2'; 
    var url = ENDPOINT + '/search/tickets';  
    var options = {      'method': 'get', muteHttpExceptions: true,'headers': headers,'payload' : JSON.stringify(data)};
    var response = UrlFetchApp.fetch(url, options);
}

Changing the endpoint and removing the payload from options work so assuming authorization and header is fine 更改端点并从选项中删除有效负载可以正常工作,因此假设授权和标头都可以

var url = ENDPOINT + '/tickets';  
var options = {'method':'get','headers':headers, muteHttpExceptions: true};

Using postman this works 使用邮递员这有效

https://xxxxxxx.freshdesk.com/api/v2/search/tickets?query="priority:3"

with header set as 标头设置为

Content-Type:application/json
Authorization:Basic xxxxxxxxxxxxxxxx

You are sending a GET request to the API with the Payload variable in the options. 您正在使用选项中的Payload变量将GET请求发送到API。 In my opinion payloads are used for POST requests. 我认为有效载荷用于POST请求。

Construct the URL with the query parameters and send it without the Payload. 使用查询参数构造URL,然后将其发送而无需有效负载。 Example: ' https://domain.freshdesk.com/api/v2/search/tickets?query= "priority:3"' 例如:“ https://domain.freshdesk.com/api/v2/search/tickets?query= “ priority:3”“

See details here: HTTP GET with request body Freshdesk API Doc: https://developers.freshdesk.com/api/#filter_tickets 在此处查看详细信息: 带有请求正文的HTTP GET Freshdesk API文档: https : //developers.freshdesk.com/api/#filter_tickets

Two issues found 1) web site does not support payload based get. 发现两个问题1)网站不支持基于有效负载的get。 2) google apps doesn't support special characters in url. 2)Google应用不支持url中的特殊字符。

Adding parameters to the original url and encoding the double quotes works. 将参数添加到原始url并编码双引号即可。

  var ENDPOINT = 'https://xxxxxx.freshdesk.com/api/v2';   
  var query ='query='+encodeURIComponent("\"priority")+":1"+encodeURIComponent("\"");
  var url = ENDPOINT + '/search/tickets?'+query;  
  var options = {'method': 'get', muteHttpExceptions: true,'headers': headers}; 
  var response = UrlFetchApp.fetch(url, options);

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

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