1. Kayako Download customers: we will continue to develop and support Kayako Download beyond July 2017, alongside the new Kayako for existing customers.

    Find out more.

  2. The forum you are viewing relates to Kayako Classic. If you signed up or upgraded to the new Kayako (after the 4th July 2016), the information in this thread may not apply to you. You can visit the forums for the new Kayako here.

Classic ASP and kayako API

Discussion in 'Developing, APIs and extending' started by mrfusion, Nov 22, 2011.

  1. mrfusion

    mrfusion New Member

    Hi,
    Has anyone successfully integrated the API with classic asp?

    I've written this so far

    Code:
    Dim xmlHttp,xmldom,salt,signature,encodedSignature
    Set xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
    set xmldom = server.CreateObject("Microsoft.XMLDOM")
    
    apikey="90d83596-150a-56a4-f5f7-4sdzb7f05d12"
    secretkey="MjIxN2RmZWYtZGYxZi05YzM0LTQ5sda12Y5ZjJiODRhMzIwMzYwOTY4NDItZTExNi0wNzQ0LTU5MDItMTg4YmE4NzllOWU4"
    salt=generatePassword(6)
    signature= sha256(salt&secretkey)
    encodedSignature =Base64Encode(signature)
    encodedSignature = server.URLEncode(encodedSignature)
    
    XMLHTTP.Open "POST", "http://mydomain.kayako.com/api/index.php?e=/Core/Test", apikey, salt, false
    xmlHTTP.setRequestHeader "Content-Type", "text/xml"
    xmlHTTP.setRequestHeader "Authorization", "Basic " & Base64Encode(apikey & ":" & secretkey )
     xmlHTTP.send 
    sha256 is a function that should be an equivalent to the one in the sample php code.
    Salt also is a function to generate a random password.

    This code shoots back 401 unauthorized. Not sure where to debug this. The request headers are probably bad i suppose.
     
  2. Rini

    Rini New Member

    I'm setting up API with asp classic. I now have successfully created a GET request. The apikey, salt and signature should be in the url and you should remove the Authorization request header.
     
  3. Rini

    Rini New Member

    Here a function to post data using ASP classic. Below the function there is a example of posting a ticket in Kayako, using this function.

    Function PostFormdata(Url, Data)
    Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP")
    httpRequest.Open "POST", Url, False
    httpRequest.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    httpRequest.Send sata
    PostFormdata = httpRequest.Status & " " & httpRequest.ResponseText
    End Function

    ' Here is what we need to get authorized
    apiKey = "apikey"
    salt = "salt"
    signature ="signature"

    ' We send the data to the following URL. In this example we create a new ticket
    strUrl = "https://example.domain.com/api/index.php?e=/Tickets/Ticket"

    'Now define the authorization string
    strAuthorization = "apikey=" & apikey &"&salt=" & salt & "&signature="& signature

    'Here we define the string that contains the data that we need to post.
    strData = "&subject=" & strSubject & "&fullname=" & strFullname & "&email=" & stremail & "&contents="& strContents & "&departmentid=" & numDepartmentID & "&ticketstatusid=" & numTicketStatusID & "&ticketpriorityid="& numTicketPriorityID& "&tickettypeid="& numTicketTypeID & "&autouserid=" & numAutoUserID & "&staffid=" & numStaffID & "&ignoreautoResponder="& numIgnoreAutoResponder

    ' Now prepare the string we will send. Consists of Authorization and the Data string
    strPostData = strAuthorization & strData

    ' Now we are ready to call the function.
    CreateTicket= PostFormdata(strUrl, strPostData)

    ' Read back the result
    response.write CreateTicket

    That's it!
    You can easily customize this for other API calls. Modify strData with the data you want to send and modify strURL with the URL you want to post the data.
     
  4. David Lerche

    David Lerche New Member

    I tried above but found that there was missing some info.
    Here is my testet solution.

    You need to download the following sha256.wsc file for hashing and base64 encode your SALT here:
    https://forums.aws.amazon.com/servlet/JiveServlet/download/9-34858-139271-2601/sha256.wsc

    Found in forum thread here:
    http://stackoverflow.com/questions/14126243/hash-hmac-in-using-pure-classic-asp


    <%
    strBaseURL = "https://your.kayakodomain.com"

    strSubject = "My test"
    strFullname = "YOUR NAME"
    stremail = "YOUR@EMAIL.com"
    strContents = "My test content - delete me!"
    numDepartmentID = "1" 'check ID's in tables in the DB
    numTicketStatusID = "1" 'check ID's in tables in the DB
    numTicketPriorityID = "1" 'check ID's in tables in the DB
    numTicketTypeID = "1" 'check ID's in tables in the DB
    numAutoUserID = "1" 'check ID's in tables in the DB
    numstaffid = "1" 'check ID's in tables in the DB

    Function generatePassword(passwordLength)'Our Function generatePassword accepts one parameter 'passwordLength'
    'Declare variables
    Dim sDefaultChars
    Dim iCounter
    Dim sMyPassword
    Dim iPickedChar
    Dim iDefaultCharactersLength
    Dim iPasswordLength
    'Initialize variables
    sDefaultChars="abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789"
    iPasswordLength=passwordLength
    iDefaultCharactersLength = Len(sDefaultChars)
    Randomize'initialize the random number generator
    'Loop for the number of characters password is to have
    For iCounter = 1 To iPasswordLength
    'Next pick a number from 1 to length of character set
    iPickedChar = Int((iDefaultCharactersLength * Rnd) + 1)
    'Next pick a character from the character set using the random number iPickedChar
    'and Mid function
    sMyPassword = sMyPassword & Mid(sDefaultChars,iPickedChar,1)
    Next
    generatePassword = sMyPassword
    End Function


    Function PostFormdata(Url, Data)
    Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP")
    httpRequest.Open "POST", Url, False
    httpRequest.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    httpRequest.Send Data
    PostFormdata = httpRequest.ResponseText
    End Function




    ' Here is what we need to get authorized
    apiKey = "In the administrator control panel (under REST API)"
    secretkey="In the administrator control panel (under REST API)."
    salt = generatePassword(16)
    ' ### be sure to have sha256.wsc in the same folder as this script
    Dim sha256
    Set sha256 = GetObject( "script:" & Server.MapPath("sha256.wsc") )
    sha256.hexcase = 0
    signature = sha256.b64_hmac_sha256( secretkey, salt )
    encodedSignature = server.URLEncode(encodedSignature)


    'We send the data to the following URL. In this example we create a new ticket
    strUrl = strBaseURL & "/api/index.php?e=/Tickets/Ticket"

    'Now define the authorization string
    strAuthorization = "apikey=" & apikey &"&salt=" & salt & "&signature="& Signature

    'Here we define the string that contains the data that we need to post.
    strData = "&subject=" & strSubject & "&fullname=" & strFullname & "&email=" & stremail & "&contents="& strContents & "&departmentid=" & numDepartmentID & "&ticketstatusid=" & numTicketStatusID & "&ticketpriorityid="& numTicketPriorityID& "&tickettypeid="& numTicketTypeID & "&autouserid=" & numAutoUserID & "&staffid=" & numStaffID & "&ignoreautoResponder="& numIgnoreAutoResponder & "&type="& strType

    'Now prepare the string we will send. Consists of Authorization and the Data string
    strPostData = strAuthorization & strData

    'Now we are ready to call the function.
    CreateTicket= PostFormdata(strUrl, strPostData)

    'Read back the result
    response.write CreateTicket

    'Read the XML string and get the ticket id created
    Dim xml: Set xml = Server.CreateObject("MSXML2.DOMDocument.3.0")
    xml.LoadXml(CreateTicket)

    Dim sResult: sResult = ""

    Dim node
    For Each node in xml.selectSingleNode("/tickets/ticket/posts/post/ticketid").childNodes
    sResult = sResult & node.xml
    Next

    'Redirect to the new ticket
    Response.Redirect strBaseURL & "/staff/index.php?/Tickets/Ticket/View/" & Left(Right(Server.HTMLEncode(sResult),11),5)

    %>
     

Share This Page