title: Make POST request to zendesk api using google apps-script
body: <p>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.</p>
  
  <p>My problem is, my authentication keeps failing.</p>
  
  <p>I have confirmed my api token is valid using Curl and python script.</p>
  
  <p>Here is a simple version of my apps-script code:</p>
  
  <pre><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);
  }
  </code></pre>
  
  <p><strong>Modified Apps script code</strong></p>
  
  <pre><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);
  }
  </code></pre>
  
  <p>Response:
  <code>401</code>
  <code>{"error":"Couldn't authenticate you"}</code></p>
  
  <p>My python script that works:</p>
  
  <pre><code>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.')
  </code></pre>