Making API Calls to VEND

Discussion in 'JavaScript' started by serialentre, Nov 13, 2015.

  1. #1
    Hi all,

    I have been trying to make an API call to Vend's API.

    Essentially, I have done step 1, and am stuck at step 2.

    Firstly,

    https://developers.vendhq.com/documentation/intro.html

    The Vend API currently only supports JSON encoded data as requests and responses. Be sure to set both the Content-Type and Accept headers to application/json to identify the request and response format.

    Secondly, for this call, I am told the following -

    https://developers.vendhq.com/documentation/oauth.html

    NOTE: Those parameters should be sent as “application/x-www-form-urlencoded” encoded body of aPOST request. They should not be sent as GET parameters in a query string.

    <!DOCTYPE html>
    <html>
        <head>
        <title>API</title>
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
        <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
        </head>
        <body>
    
    <script>
    
    $(document).ready(function() {
    
      $.ajax({
        type: 'POST',
        url: 'https://firststore.vendhq.com/api/1.0/token',
        dataType: 'json',
        accepts: 'application/json', // accepts json response
        beforeSend: function(req) {
                  req.setRequestHeader("Access-Control-Allow-Origin", "*");
                  },
        crossDomain: true,
        contentType: "application/x-www-form-urlencoded", // send as x-www-form-urlencoded
        data: "code=abc&client_id=abc&client_secret=abc&grant_type=authorization_code&redirect_uri=http://testtesttest.com,
        success: function(success) {
          console.log('hello!');
       },
        error: function(e) {
          //called when there is an error
          console.log('error');
        },
      });
    });
    
    </script>
    
    </body>
    </html>
    
    Code (markup):
    The error that was throw back in the browser console is this -

    XMLHttpRequest cannot load https://firststore.vendhq.com/api/1.0/token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://testtesttest.com' is therefore not allowed access. The response had HTTP status code 401.

    Can someone advice as to if I am doing it wrongly, and if so, how should I approach this API call?

    Thank you!
     
    serialentre, Nov 13, 2015 IP