我想向 zendesk api 发出一个 POST 请求,每次提交谷歌表单时,它将从谷歌表格创建 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);
}
修改应用脚本代码
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.')