Rest API call

dinesh 41 Reputation points
2021-03-02T08:39:09.207+00:00

Hi, I want to get token and site id from tableau URL API. Then I found something on stackoverflow but getting an error at tableauCredentialsType, tsRequest and siteType. Is this seperate dll? or what it is? Could someone help me

namespace TableauAPI
{
class Program
{
static HttpClient client = new HttpClient();
public static string site_id { get; set;}
public static string token { get; set; }
static void Main(string[] args)
{
CallWebAPIAsync().Wait();
}
static async Task CallWebAPIAsync()
{
using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
client.BaseAddress = new Uri("https://my server url.com");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
SignIn(client);
Workbooks(client, site_id, token);
}
Console.Read();
}
public class CustomXmlMediaTypeFormatter : XmlMediaTypeFormatter
{
public CustomXmlMediaTypeFormatter()
{
UseXmlSerializer = true;
WriterSettings.OmitXmlDeclaration = false;
}
}
public static tableauCredentialsType SignIn(HttpClient client)
{
var name = "Administrator";
var password = "password";
var site = "";
var tableauCredentialsType = new tsRequest()
{
Item = new tableauCredentialsType
{
name = name,
password = password,
site = new siteType() { contentUrl = site }
}
};
var httpContent = new ObjectContent<tsRequest>(tableauCredentialsType, new CustomXmlMediaTypeFormatter());
var httpResponseMessage = client.PostAsync("/api/3.2/auth/signin", httpContent).Result;
if (httpResponseMessage.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception(string.Format("Unable to login to the server", site, name));
var responseLogin = httpResponseMessage.Content.ReadAsAsync<tsResponse>(new List<MediaTypeFormatter>() { new CustomXmlMediaTypeFormatter() }).Result;
var resultTableauCredentialsType = ((tableauCredentialsType)responseLogin.Items[0]);
site_id = resultTableauCredentialsType.site.id;
token = resultTableauCredentialsType.token;
return resultTableauCredentialsType;
}
public static IList<workbookType> Workbooks(HttpClient client, string siteid, string auth_token, int pageSize = 100, int pageNumber =1)
{
IList<workbookType> results = new List<workbookType>();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("x-tableau-auth", auth_token);
var query = client.GetAsync(string.Format("/api/3.2/sites/{0}/workbooks", siteid, pageSize, pageNumber)).Result;
if(query.StatusCode == System.Net.HttpStatusCode.OK)
{
var result = query.Content.ReadAsAsync<tsResponse>(new List<MediaTypeFormatter>() { new CustomXmlMediaTypeFormatter() }).Result;
var pagination = (paginationType)result.Items[0];
var workbooks = ((workbookListType)result.Items[1]).workbook.AsEnumerable();
for (int i=2; i < pageNumber; i++)
{
query = client.GetAsync(string.Format("/api/3.2/sites/{0}/workbooks", siteid, pageSize, i)).Result;
if(query.StatusCode == System.Net.HttpStatusCode.OK)
{
result = query.Content.ReadAsAsync<tsResponse>(new List<MediaTypeFormatter>() { new CustomXmlMediaTypeFormatter() }).Result;
workbooks = workbooks.Concat(((workbookListType)result.Items[1]).workbook.AsEnumerable());
}
}
results = workbooks.ToList();
}
return results;
}
}}

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,525 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,986 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-03-02T13:12:08.607+00:00
    0 comments No comments

  2. Mouad Cherkaoui 6 Reputation points
    2021-03-02T19:25:02+00:00

    Hi dinesh-3328,

    the two type that causes the error are used to deserialize the response and being a type that holds it, you can simply create them with the properties needed, for example:

    var tableauCredentials = new TsRequest() // for each element we create a class representing it
    { 
       Item = new TableauCredentialsType
       {
              name = name,
              password = password,
              site = new SiteType() { contentUrl = site }
        }
    };
    

    SiteType.cs:

    public class SiteType {
         public string contentUrl { get; set; }
    }
    

    TableauCredentialsType.cs:

    public class TableauCredentialsType {
         public string name { get; set; }
         public string password { get; set; }
         public SiteType siteType { get; set; }
    }
    

    TsRequest.cs:

    public class TsRequest {
         public TableauCredentialsType Item { get; set; }
    }
    

    this was an example to demonstrate the idea, you should adapt these classes to the real response object.
    also check if there isn't a tableau sdk for dotnet, since it is more appropriate to use it since it should be more consistent with their apis and it will give you simplified methods that abstracts the http requests.

    Hope it helps!

    0 comments No comments