Thursday, 19 March 2015

Creating a Tableau (API V 8) Ticket ASP.Net MVC 5

If you are using your own Tableau server then in order to show a viz on your page, you'll need to create a trusted ticket. You'll need to put this trusted ticket in your viz url and only then you'll get access to it.

In order to get the ticket, you'll also need to create a user account on the Tableau server that has enough permissions to execute the call.

Step #1 - Creating a Trusted Authentication:

When you embed Tableau Server views into webpages, everyone who visits the page must be a licensed user on Tableau Server. When users visit the page they are prompted to sign in to Tableau Server before they can see the view. If you already have a way of authenticating users on the webpage or within your web application, you can avoid this prompt and save your users
from having to sign in twice by setting up trusted authentication.....

Tableau has a feature to let a Tableau Server defer authentication to an external server, like a front end webserver or portal. Read about trusted authentication in the Tableau Server documentation and knowledge base articles.
http://onlinehelp.tableausoftware.com/current/server/en-us/trusted_auth.htm

Step #2 - Web.Config:

You can either place the following in the web.config file or in your helper class where the main method will be. I am taking the web.config route.

<add key="TableauServerUrl" value="http://data.tableauserver.com/trusted"/>
<add key="TableauServerUser" value="TableauServerUser"/>

Step #3 - Getting the Ticket:

You'll need to do an http post to get the ticket

public static string GetTableauTicket()
{
    var ticket = String.Empty;
    var tableauServerUrl = ConfigurationManager.AppSettings["TableauServerUrl"];
    var tableauServerUser = ConfigurationManager.AppSettings["TableauServerUser"];
    if (!String.IsNullOrWhiteSpace(tableauServerUrl) && !String.IsNullOrWhiteSpace(tableauServerUser))
    {
        try
        {
            using (var client = new WebClient())
            {
                var values = new NameValueCollection();
                values["username"] = tableauServerUser;
                var response = client.UploadValues(tableauServerUrl, values);
                var responseString = Encoding.Default.GetString(response);
                //-1 gets returned if user being used does not have enough permissions
                if (!String.IsNullOrWhiteSpace(responseString) && responseString != "-1")
                {
                    ticket = responseString;
                }
            }
        }
        catch (Exception ex)
        {
            //Log Exception
        }
    }
    return ticket;
}


Step #4 - Specifying the Ticket in the Viz URL:

If your viz url is like
http://tabserver/views/workbookname/viewname

Then your viz url will be like after placing the ticket in it
http://tabserver/trusted/[PLACE-TICKET-HERE]/views/workbookname/viewname