我想向zendesk api发送POST请求,该请求将在每次提交Google表单时从Google表格创建zendesk票证。
我的问题是,我的身份验证一直失败。
我已使用Curl和python脚本确认我的api令牌有效。
这是我的应用程序脚本代码的简单版本:
function onSubmit(e){
var subject = 'Test ticket';
var body = 'test ticket was successful!';
var data = {'ticket': {'subject': subject, 'comment': {'body': body}}};
var url = 'https://mydomain.zendesk.com/api/v2/tickets.json';
var user = 'myemail@email.com/token:';
var pwd = 'MY_SECRET_API_KEY';
options = {
'method' : 'post',
'headers': {
'contentType': 'application/json',
'Authorization': 'Basic ' + Utilities.base64Encode(user + pwd);
},
'data' : JSON.stringify(data)
};
UrlFetchApp.fetch(url, options);
}
修改后的Apps脚本代码
function onSubmit(e){
var subject = 'Test ticket';
var body = 'test ticket was successful!';
var data = {'ticket': {'subject': subject, 'comment': {'body': body}}};
var url = 'https://mydomain.zendesk.com/api/v2/tickets.json';
var user = 'myemail@email.com/token';
var pwd = 'MY_SECRET_API_KEY';
var options = {
'method' : 'post',
'headers': {
'Authorization': "Basic " + Utilities.base64Encode(user + ':' + pwd)
},
'payload' : JSON.stringify(data),
'contentType': 'application/json',
'muteHttpExceptions': true
};
UrlFetchApp.fetch(url, options);
}
响应:
401
{"error":"Couldn't authenticate you"}
我的python脚本有效:
import json
import requests
subject = 'This is a test!'
body = 'Testing api calls.'
data = {'ticket': {'subject': subject, 'comment': {'body': body}}}
payload = json.dumps(data)
url = 'https://mydomain.zendesk.com/api/v2/tickets.json'
user = 'myemail@email.com/token'
pwd = 'MY_SECRET_API_KEY'
headers = {'content-type': 'application/json'}
response = requests.post(url, data=payload, auth=(user, pwd), headers=headers)
if response.status_code != 201:
print('Status:', response.status_code, payload, 'Problem with the request. Exiting.')
exit()
print('Successfully created the ticket.')
答案 0 :(得分:0)
此修改如何?
请如下修改options
。
contentType
时,请将其放在headers
之外。
Content-Type
时,请将其放在headers
中。data
修改为payload
。var options = {
'method' : 'post',
'headers': {
'Authorization': 'Basic ' + Utilities.base64Encode(user + ':' + pwd), // Modified
},
'contentType': 'application/json',
'payload' : JSON.stringify(data)
};
如果这不起作用,请告诉我。我想修改它。