How to downlopad workbook from tableau retain folder

`import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

public class TableauWorkbookDownloader {

    private static final String SERVER_URL = "https://your-tableau-server.com";
    private static final String API_VERSION = "3.9";
    private static final String USERNAME = "your_username";
    private static final String PASSWORD = "your_password";
    private static final String SITE_ID = ""; // Leave empty for the default site

    public static void main(String[] args) {
        String tag = "your_tag";
        String token = authenticate();
        if (token != null) {
            Map<String, String> workbookMap = getWorkbooksByTag(token, tag);
            if (!workbookMap.isEmpty()) {
                workbookMap.forEach((workbookId, folderPath) -> downloadWorkbook(token, workbookId, folderPath));
            } else {
                System.out.println("No workbooks with the specified tag found.");
            }
        }
    }

    private static String authenticate() {
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            URI uri = new URI(SERVER_URL + "/api/" + API_VERSION + "/auth/signin");
            HttpPost post = new HttpPost(uri);
            String json = new JSONObject()
                    .put("credentials", new JSONObject()
                            .put("name", USERNAME)
                            .put("password", PASSWORD)
                            .put("site", new JSONObject().put("contentUrl", SITE_ID)))
                    .toString();
            post.setEntity(new StringEntity(json));
            post.setHeader("Content-Type", "application/json");

            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity);
            JSONObject jsonResponse = new JSONObject(responseString);
            return jsonResponse.getJSONObject("credentials").getString("token");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static Map<String, String> getWorkbooksByTag(String token, String tag) {
        Map<String, String> workbookMap = new HashMap<>();
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            URI uri = new URI(SERVER_URL + "/api/" + API_VERSION + "/sites/" + SITE_ID + "/workbooks?filter=tags:eq:" + tag);
            HttpGet get = new HttpGet(uri);
            get.setHeader("X-Tableau-Auth", token);

            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity);
            JSONObject jsonResponse = new JSONObject(responseString);
            JSONArray workbooks = jsonResponse.getJSONObject("workbooks").getJSONArray("workbook");

            for (int i = 0; i < workbooks.length(); i++) {
                JSONObject workbook = workbooks.getJSONObject(i);
                String workbookId = workbook.getString("id");
                String projectId = workbook.getJSONObject("project").getString("id");
                String folderPath = getProjectPath(token, projectId);
                workbookMap.put(workbookId, folderPath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return workbookMap;
    }

    private static String getProjectPath(String token, String projectId) {
        StringBuilder folderPath = new StringBuilder();
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            while (projectId != null && !projectId.isEmpty()) {
                URI uri = new URI(SERVER_URL + "/api/" + API_VERSION + "/sites/" + SITE_ID + "/projects/" + projectId);
                HttpGet get = new HttpGet(uri);
                get.setHeader("X-Tableau-Auth", token);

                HttpResponse response = client.execute(get);
                HttpEntity entity = response.getEntity();
                String responseString = EntityUtils.toString(entity);
                JSONObject jsonResponse = new JSONObject(responseString);
                JSONObject project = jsonResponse.getJSONObject("project");
                folderPath.insert(0, File.separator + project.getString("name"));
                projectId = project.optString("parentProjectId", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return folderPath.toString();
    }

    private static void downloadWorkbook(String token, String workbookId, String folderPath) {
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            URI uri = new URI(SERVER_URL + "/api/" + API_VERSION + "/sites/" + SITE_ID + "/workbooks/" + workbookId + "/content");
            HttpGet get = new HttpGet(uri);
            get.setHeader("X-Tableau-Auth", token);

            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();

            Files.createDirectories(Paths.get("." + folderPath));
            String filePath = "." + folderPath + File.separator + workbookId + ".twb";

            try (InputStream in = entity.getContent();
                 OutputStream out = new FileOutputStream(filePath)) {
                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
            }
            System.out.println("Workbook downloaded successfully to " + filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}`

Authentication: The authenticate method sends a POST request to Tableau’s sign-in endpoint with the username and password, and it returns the authentication token.

Finding Workbooks by Tag: The getWorkbooksByTag method sends a GET request to fetch workbooks filtered by the specified tag. It then calls getProjectPath to get the folder path for each workbook and stores the workbook IDs and their corresponding folder paths in a map.

Getting Project Path: The getProjectPath method recursively traverses the project hierarchy to build the full folder path for each project.

Downloading the Workbook: The downloadWorkbook method sends a GET request to download the workbook content. It creates the necessary directories and saves the workbook to the appropriate folder.

To download a workbook from Tableau using Java, you’ll need to interact with the Tableau Server REST API. The process involves authenticating with the server, finding the workbook by its tag, and then downloading it.

Here is an example Java code to achieve this:

Add necessary dependencies: You’ll need a JSON library like org.json and an HTTP client library like Apache HttpClient.

Code to authenticate, search by tag, and download the workbook:

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật