Unable to return user favorites via Tableau REST API
Asked Answered
S

2

9

UPDATE: Sept 2019.

This API call now works as intended. Issues on the Tableau end appear to have been resolved and the call now returns the correct data.

===============================================================

I'm using the Tableau REST API via C# to try and get a list of users favorites. I know the user has some, because its me. I have tried using API Version 2.8,3.0, 3.1 and 3.2 with little to no joy. 2.8 and 3.0 respond with:

<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.8.xsd"> //3.0.xsd when using API Version 3.0
     <favorites/> //There should be a plethora of favorites of all varieties in here.
</tsResponse>

3.1 and 3.2 give me a (404) Not found.

The code i have in c# is:

public static string QueryFavourites(string APIVersion, string AuthToken, string SiteID, string UserID)
    {
        string result = "";
        try
        {
            string url = $@"{Server}/api/{APIVersion}/sites/{SiteID}/favorites/{UserID}";
            // Create the web request 
            WebRequest request = WebRequest.Create(url) as WebRequest;
            request.PreAuthenticate = true;
            request.Headers.Add($"x-tableau-auth: {AuthToken}");

            // Get response 
            using (WebResponse response = request.GetResponse())
            {
                // Get the response stream 
                StreamReader reader = new StreamReader(response.GetResponseStream());

                // Read the whole contents and return as a string 
                result = reader.ReadToEnd();
            }
            return result;
        }
        catch(Exception E)
        {
            logger.Error("Error! System Says: " + E.Message);
            return result;
        }
    }

I know the method works, as it is used for multiple other API calls using a different URL for each (depending on the call). Does anyone know if its an issue on the tableau end or on my end? Apparently it should work with Tableau server 2.8 or above, which we have. (i think we're running 2018.1)

Is anyone able to get a list of favorites for a user using tableau REST API? Where am i going wrong?

(I have also posted the question on Tableau Forum.)

UPDATE:

I have included the CURL and Headers of the request, as well as the results, in the screenshots below. (I use 'Restlet Client' more than 'Postman' so screenshots are from the former.) ID's and authentication tokens have been removed as they are sensitive information, and i don't think my company would be happy with me putting them on the public facing internet. All ID's and auth keys are in the correct case and presented correctly. They are used in several other API calls with success and are pulled direct from Tableau via the API.

The exceptions, i have found out are the inability to find the version of the API that i am calling. so v2.6 - v2.8 and v3.0 all "work". Other versions return a 404001 VERSION_NOT_FOUND error.

enter image description here

enter image description here

Substantialize answered 4/1, 2019 at 9:22 Comment(9)
I'm not familiar with C# strings but I don't see where in your function code you replace the bracketed values for Server, APIVersion, SiteID, and UserID in the url variable.Accordance
The word within the brackets is the variable itself and is set elsewhere in the code. SiteID, UserID, APIVersion, and AuthToken are all passed in as parameters. "public static string QueryFavourites(string APIVersion, string AuthToken, string SiteID, string UserID)"Substantialize
This may be the part that's throwing you. The bracketed values are the variables and are inserted with string interpolation (The $ sign before the string).Substantialize
Perhaps you should re-check if the information you supply is correct: for example try to craft the desired request in postman, and getting a desirable result before implementing it.Curet
Tried postman. CURL runs fine, just returns no 'favorites' data (i.e. <favorites/> output should look like this). The information is good, Server, SiteID, and UserID are all used in other API calls with success. All other API calls are of similar structure to this one. Tableau API just seems to be a bit... touchy...Substantialize
Can you show us the headers tab of Postman for the request and the response?Slurry
Also on the same documentation page you provided, there are two errors corresponded with 404 status code: "Site not found" and "User not found". It is very possible that the result still contains an empty tag but the status code is 404, therefore, C# throwing an exception.Slurry
You can also cast your exception to WebException and retrieve the body of error response to see what it contains. Also, make sure your UserId and SiteId are both lower case and have a valid format (GUID). These are the thing I suspect regarding your code.Slurry
Updated with screenshots. Will most likely be Monday before i can reply again as i am about to clock off, but i am very grateful for all your ideas and assistance.Substantialize
C
5

The approach i would take is:

  1. Query a user on the site. (the user that has the favorites)

  2. Check if the user is actually: the same user you are authenticated as; and the same user you are gonna query for favorites

  3. If they are the same, try adding a favorite with the REST API (DataSource, View or Workbook)

  4. Get the favorites for the user, the datasource/view/workbook you added as a favorite should be in there.

If you want to Update the user, Add user to site or Add user to Group, I've added links to the documentation

You can do these things with Postman/tool of your choice.

What you can also try is ensuring the user that is querying another user (or the same) is a server admin (just to be safe), and making sure that you are a member of the same site of another (or the same) user.

Hope this helps!

EDIT: Maybe you can try adding a new user with group regular to a site, ensuring that you are a member of the site too. Afterwards adding a favorite and getting the favorites for the user of group regular. If that doesnt work u can verify whether its impossible to get favorites for users of group regular as well, besides admins.

Curet answered 5/2, 2019 at 16:9 Comment(5)
When i read this, i thought you had it with points 3 and 4. I figured it may only be retrieving the favorites that are set by the API as there is no option when manually setting a favorite for adding a "label". But no. still nothing. Added the favorite via the API, Response code indicated all good. Favorite not set. Not in the API response for 'Get Favorites' and not in the favorites list on the site.Substantialize
The user account querying is High level admin and has API permissions for everything, so its definitely not a permissions issue, definitely the same site that is being queried that the user and favorites relate to, user being queried definitely exists with favorites (it's me, also admin account). I'm beginning to get the feeling that the "favorites" feature in the API is broken. Not even Tableau support are replying to me.Substantialize
Hey, im glad it's working now. However, since the solution for this question was that - the user you're getting favorites for has to be the same user as you're authenticated as - and I've pointed out that you should verify whether this is the case, would you be so kind to mark it as the correct answer? In general I think it's nice if answered questions can be closed, considering the issue has been resolved.Curet
If you need any additional information in the answer, let me know so I can add it.Curet
I have absolutely no issues marking you as the answer. Apologies for not doing it sooner. Not only did you get it, you were also the only one (on both Stack Overflow and Tableau Forums) to offer a suggestion. Thank you Lennart!Substantialize
S
1

Finally found out what was happening. It doesn't work as intended. It will only return user favorites for the user that is authenticated in the authentication token, regardless of what user id you put in the request. Had a call with Tableau support and accidentally figured it out, when we switched authenticated user. I will leave this here in case anyone else comes across the same issue.

Substantialize answered 11/2, 2019 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.