API Create User (with Company) in VB .net 4.5

  • 20 September 2013
  • 5 replies
  • 146 views

Been searching around and cannot find the answer to either of my problems.


a) can i create a new user with a company in one go via API or does it need to be 2 separate posts? (a customer and then user?)


b) i am using VB .NET and although i can find many examples with PHP none with VB,


I am sure it is simple but everything i tried receives error 406:Not Acceptable at this point i am unsure if it is my XML string builder or my credentials but i was hoping that someone would have experience in VB.


Here is my code:

Shared Function Create() As String
Dim n As String
Dim em As String
Dim c As String
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim address As Uri
Dim data As StringBuilder
Dim byteData() As Byte
Dim postStream As Stream = Nothing
Dim results As String
n = "TESTS"
em = "email@email.com"
c = "clientref"
address = New Uri("https://****.freshdesk.com/contacts.xml")
request = DirectCast(WebRequest.Create(address), HttpWebRequest)

request.Method = "POST"
request.ContentType = "application/xml"
request.Credentials = New NetworkCredential("APIKEY", "x")
request.Accept = "*/*"

' Create the data we want to send (each data.Append is for specific paramater data)
data = New StringBuilder()
data.Append("<?xml version=""1.0"" encoding=""UTF-8""?>")
data.Append("<user>")
data.Append("<email>" + em + "</email>")
data.Append("<name>" + n + "</name>")
data.Append("</user>")

' WebUtility.UrlEncode
' Create a byte array of the data we want to send
byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())

' Set the content length in the request headers
request.ContentLength = byteData.Length

' Write data
Try
postStream = request.GetRequestStream()
postStream.Write(byteData, 0, byteData.Length)

response = request.GetResponse
Dim receiveStream As Stream = response.GetResponseStream
Dim rdr As New StreamReader(receiveStream, System.Text.Encoding.ASCII)
Dim strResp As String = rdr.ReadToEnd

Finally
If Not postStream Is Nothing Then postStream.Close()
End Try
End Function


Many thanks in advance

This topic has been closed for comments

5 replies

FYI we managed to solve this problem. 

The authorisation section must be converted to 64bit string, then the api works.


To add company to a user:

Company must be set up first

<?xml version="1.0" encoding="UTF-8"?>

<customer>

 

 <name>Company Name</name> <!--(Mandatory)-->

</customer>

To https://ecustoms.freshdesk.com/customers.xml

then ID returned.

Once company set and you know ID

In new user created include <customer-id>Returned ID</customer-id>

Hi Mark

Could you post your working solution - I am struggling with a similar problem with Tickets and c#?


Thanks,

Graeme

One of the programers i work with cracked it, they included;

Public credidentials As String = "API:X"


then a "Send freshdesk" function


Dim wHeader As WebHeaderCollection = New WebHeaderCollection()


        wHeader.Clear()


Dim Authorization As String = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(credidentials))
        wHeader.Add("Authorization: Basic " + Authorization)


Dim wRequest As HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(sUrl), HttpWebRequest)


        wRequest.Headers = wHeader
        wRequest.ContentType = "application/xml"
        If bMessage Is Nothing Then
            wRequest.ContentLength = 0
        Else
            wRequest.ContentLength = bMessage.Length
        End If
        wRequest.Method = sMethod
        wRequest.Accept = "*/*"

Thanks Mark, I have it working now.

For anyone else, here is a C# example that gets all tickets. It sits behind a form with a textbox and a button on it.

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace Freshdesk_GHExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string strCredidentials = "MyAPIKey:X";
                WebHeaderCollection oHeader = new WebHeaderCollection();
                oHeader.Clear();
                string strAuthorization = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(strCredidentials));
                oHeader.Add("Authorization: Basic " + strAuthorization);
                HttpWebRequest oRequest;
                HttpWebResponse oResponse = null;
                Uri oURI;
                StringBuilder oData;
                Byte[] byteData;
                oURI = new Uri("https://MyCompany.freshdesk.com/helpdesk/tickets.xml");
                oRequest = (HttpWebRequest)WebRequest.Create(oURI);
                oRequest.Headers = oHeader;
                oRequest.ContentLength = 0;
                oRequest.Method = "GET";
                oRequest.ContentType = "application/xml";
                oRequest.Accept = "*/*";
                // WebUtility.UrlEncode
                // Create a byte array of the data we want to send  
                oData = new StringBuilder();
                byteData = UTF8Encoding.UTF8.GetBytes(oData.ToString());
                // Set the content length in the request headers  
                oRequest.ContentLength = byteData.Length;
                oResponse =(HttpWebResponse) oRequest.GetResponse();
                Stream oRXStream = oResponse.GetResponseStream();
                StreamReader oReader = new StreamReader(oRXStream, System.Text.Encoding.ASCII);
                string strResp = oReader.ReadToEnd();
                textBox1.Text = strResp;
            }
            catch (Exception ex)
            {
                textBox1.Text=ex.ToString();
            }
        }// button click
    }// form1
}

Userlevel 4
Badge +12





Folks,




You can find more API repos here : github.com/freshdesk/fresh-samples




Cheers!