I want to make a POST request to the zendesk api which will create zendesk tickets from google sheets every time a google form is submitted.
My problem is, my authentication keeps failing.
I have confirmed my api token is valid using Curl and python script.
Here is a simple version of my apps-script code:
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);
}
Modified Apps script code
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);
}
Response: 401
{"error":"Couldn't authenticate you"}
My python script that works:
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.')
How about this modification?
Please modify options
as follows.
contentType
, please put it to outside of headers
.
Content-Type
, please put it in the headers
.data
to payload
.var options = {
'method' : 'post',
'headers': {
'Authorization': 'Basic ' + Utilities.base64Encode(user + ':' + pwd), // Modified
},
'contentType': 'application/json',
'payload' : JSON.stringify(data)
};
If this didn't work, please tell me. I would like to modify it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.