3 Replies Latest reply on Jan 31, 2017 4:59 PM by erik lundberg

    REST API using C#

    Dan Pierce

      Hi All,

       

      I've been following the documentation on Tableau's API Reference page (partial screen shot below) for how to form requests to REST API on our server.  Our server is https://a0319t428.  It has several sites on it, one of which is at /t/smg-poc.  I'm trying just to confirm that we can even execute the API as shown in the documentation, using C# to signin to the server.  I've written the following console app code to test:

       

      Signin.PNG

       

      static void Main()
              {
                  System.Console.WriteLine("Getting started...");
                  System.Console.WriteLine();
      
      
                  //Create XML payload for the api call.
                  using (XmlWriter loginxml = XmlWriter.Create("login.xml"))
                  {
                      loginxml.WriteStartDocument();
                      loginxml.WriteStartElement("tsRequest");
                      loginxml.WriteStartElement("credentials");
                      loginxml.WriteAttributeString("name", "admin-username");
                      loginxml.WriteAttributeString("password", "admin-password");
                      loginxml.WriteStartElement("site");
                      loginxml.WriteAttributeString("contentUrl", "t/smg-poc");
                      loginxml.WriteEndElement();
                      loginxml.WriteEndElement();
                      loginxml.WriteEndElement();
                      loginxml.WriteEndDocument();
                  }
                  XElement myxml = XElement.Load("login.xml");
      
      
                  //Convert the XML payload to a string and display so we can check that it's well-formed
                  string myxmlstring = myxml.ToString();
                  System.Console.WriteLine(myxmlstring);
                  System.Console.WriteLine();
      
      
                  //send payload to routine to make the web request
                  string response = LoginToTableau(myxmlstring);
      
      
                  //display the response from the web request
                  System.Console.WriteLine(response);
      
      
                  System.Console.Read();
              }
              static string LoginToTableau(string xml)
              {
                  //Is this the correct url we should be sending the web request to?
                  string urltl = "https://a0319t428/api/2.0/auth/signin";
      
      
                  //Send the above url, the POST method, and the XML Payload string to create the web request
                  var infotl = SendWebRequest(urltl, "POST", xml);
      
      
                  return infotl;
              }
              static string SendWebRequest(string Url, string Method, string payload)
              {
      
      
                  string response;
      
      
                  //encode the XML payload
                  byte[] buf = Encoding.UTF8.GetBytes(payload);
      
      
                  //set the system to ignore certificate errors because Tableau server has an invalid cert.
                  System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
      
      
                  //Create the web request and add the XML payload
                  HttpWebRequest wc = WebRequest.Create(Url) as HttpWebRequest;
                  wc.Method = Method;
                  wc.ContentType = "text/xml";
                  wc.ContentLength = buf.Length;
                  wc.GetRequestStream().Write(buf, 0, buf.Length);
      
      
                  try
                  {
                      //Send the web request and parse the response into a string
                      HttpWebResponse wr = wc.GetResponse() as HttpWebResponse;
                      Stream receiveStream = wr.GetResponseStream();
                      StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
                      response = readStream.ReadToEnd();
      
      
                      receiveStream.Close();
                      readStream.Close();
                      wr.Close();
      
      
                      readStream.Dispose();
                      receiveStream.Dispose();
                  }
                  catch (WebException we)
                  {
                      //Catch failed request and return the response code
                      response = ((HttpWebResponse)we.Response).StatusCode.ToString();
                  }
                  return response;
      
      
              }
      

       

      When I execute the code above as-is, I get an Internal Server Error response.  If I remove the site info from the uri and send the request to https://a0319t428/api/2.0/auth/signin I get a 404 error, which the reference doc says is a user not found response, but I can see my user listed and with a System Admin role.

       

      What am I doing wrong?

      Thanks,
      Dan