简体   繁体   English

Tableau REST API:使用Javascript获取令牌

[英]Tableau REST API: Using Javascript to get the Token

I am a complete beginner with REST API and I could not figure out how I am to proceed. 我是REST API的入门者,我不知道该如何进行。 I installed Postman and was successfully able to get the Token, but I am not sure how to send the raw XML payload in javascript. 我安装了Postman并成功获得了令牌,但是我不确定如何在javascript中发送原始XML有效内容。

 <tsRequest>
      <credentials name ="XXX" password="YYY"  >
           <site contenturl = "" />
      </credentials>
 </tsRequest>

I have : 我有 :

 httpRequest.open('POST', 'http://MY-SERVER/api/2.4/auth/signin', false);
 httpRequest.setRequestHeader("Content-type", "application/xml");

Not sure how to add the xml payload. 不知道如何添加xml有效负载。 I have access to a Tableau Server(MY-SERVER) and everything. 我可以访问Tableau Server(MY-SERVER)以及其他所有内容。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Thank you! 谢谢!

You are getting closer, you just need to use the send method to send your XML: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send 距离越来越近,您只需要使用send方法发送XML: https : //developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send

Just make sure that your XML is properly encoded in javascript when you're inputting it. 只需在输入时确保将XML正确编码为javascript。 So if you are using double quotes inside your XML, make sure you have single quotes to declare your string in javascript (eg) var data = '<credentials name="XXX" >'; 因此,如果您在XML中使用双引号,请确保使用单引号在javascript中声明字符串(例如var data = '<credentials name="XXX" >';

Related: Send POST data using XMLHttpRequest 相关: 使用XMLHttpRequest发送POST数据

In addition to @AnilRedshift answer, here's the functioning code: 除了@AnilRedshift答案之外,以下是起作用的代码:

login_details=[];
function getToken() {

var url = "http://yourServerAddress/api/2.0/auth/signin";
var params = "<tsRequest><credentials name='Username' password='UserPassword' ><site contentUrl='' /></credentials></tsRequest>";

    return zuo = new Promise(function(resolve,reject){

            var xhr = new XMLHttpRequest(); 
            xhr.open("POST", url, true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.withCredentials = true;
            xhr.onload= function(){ 
                if (this.status === 200) {
                    var parsed_xml = JSON.parse(JSON.stringify(x2js.xml_str2json(xhr.responseText)))
                    login_details.push(parsed_xml.tsResponse.credentials._token);                   login_details.push(parsed_xml.tsResponse.credentials.site._id);
                    resolve(login_details);
                }
            }
            xhr.onerror=reject;
        xhr.send();

        })


}

function getWorkbooks(){

var url = "http://serveraddress//api/2.3/sites/"+login_details[1]+"/workbooks?pageSize=1000";

    return zuo = new Promise(function(resolve,reject){

            var xhr = new XMLHttpRequest(); 
            xhr.open("GET", url, true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.setRequestHeader("X-Tableau-Auth",login_details[0]);
            xhr.onload= function(){ 
                if (this.status === 200) {

                    var workbooks = JSON.parse(JSON.stringify(x2js.xml_str2json(xhr.responseText)))

                    for (var f=0;f<workbooks.tsResponse.workbooks.workbook.length;f++){
                        if(workbooks.tsResponse.workbooks.workbook[f].project._name=="Default"){
                            workbooks_list.push(workbooks.tsResponse.workbooks.workbook[f]._id)
                        }
                       resolve();
                    }

                }
            }
            xhr.onerror= function(){ 


                    console.log(xhr.responseText);              
            }

        xhr.send();

        })


}

Invoke the code with: 使用以下代码调用代码:

getToken()
    .then(function(login_details){
        console.log(login_details[0]+"/"+login_details[1]);
    })
    .then(function(){
        getWorkbooks();
    })

getToken() function gets the login token which has to be used in all subsequent calls. getToken()函数获取必须在所有后续调用中使用的登录令牌。 getWorkbooks() fetches all dashboards in 'Default' project but this kind of request can be used for all GET type requests. getWorkbooks()获取“默认”项目中的所有仪表板,但这种请求可用于所有GET类型的请求。

Please note that this approach uses hardcoded values for password and username which is generally not the best practice. 请注意,这种方法将硬编码的值用于密码和用户名,这通常不是最佳做法。 It would be way better to use server side scripting or encrypting (better but still with flavs). 最好使用服务器端脚本或加密(更好,但仍然使用flav)。

You can find whole step by step tutorial and running code here: http://meowbi.com/2017/10/23/tableau-fields-definition-undocumented-api/ 您可以在此处找到完整的分步教程和运行代码: http : //meowbi.com/2017/10/23/tableau-fields-definition-undocumented-api/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM