3 Replies Latest reply on Oct 30, 2017 2:45 AM by Maciek La

    Tableau REST API: Using Javascript to get the Token

    Archana Mohan

      Hello all,

       

      I am a complete beginner with REST API and I could not figure out how I am to proceed.

      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.

       

           <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. I have access to a Tableau Server(MY-SERVER) and everything.

      Any help would be greatly appreciated!

       

      Thank you,

      Archana

        • 1. Re: Tableau REST API: Using Javascript to get the Token
          patrick.byrne.0

          Archana,

           

          I moved this post to the Tableau Community specifically for the Rest API: REST API . There is a better chance someone in this space will be able to assist.

           

          I hope someone will be able to assist!

           

          Cheers

          Patrick

          • 2. Re: Tableau REST API: Using Javascript to get the Token
            Mossdiq Hasan

            Hey

             

            I just gone through your question and its a very interesting question.

            previously i done something with php and javascript to send the request in xml format.

            // Inside this field you can enter your xml request code .

            // For login you don't want any header so remove the header file .

            $xml = '<?xml version="1.0" encoding="UTF-8"?>' . 

                        '<tsRequest>' .

                            '<user id="'.$Userid.'" />'.

                        '</tsRequest>';

                // print_r($post_data);

                $content_type = 'text/xml';

                $aContext = array(

                    'http' => array(

                        'method'  => 'POST',

                        'header'  => 'X-Tableau-Auth : '.$AuthToken,

                                        'Content-type: ' . addslashes($content_type) .'\r\n'

                                        . 'Content-Length: ' . strlen($xml) . '\r\n',

                        'content' => $xml,

                    ),

                );

             

            If you want to get auth-token with the help of php,javascript & json then the below mention code will also work for you : -

             

             

            <?php

                session_start();

                $server = "Server Address";

                $user = "Username";

                $password = "Password";

                $siteId = "TableauWeb";

                $SignInUrl = "http://".$server."/api/2.5/auth/signin";

                $data = '{"credentials":{"name":"'.$user.'","password":"'.$password.'","site":{"contentUrl":"'.$siteId.'"}}}';

                // echo $data.'<br>';

                $options = array(

                  'http' => array(

                    'method'  => 'POST',

                    'content' => $data,

                    'header'=>  "accept: application/json,\r\n".

                                "content-type: application/json\r\n"

                    )

                );

                $context  = stream_context_create($options);

                $result = file_get_contents($SignInUrl,false,$context);

                if($result)

                {

                $response = json_decode($result);

                $responsearray = array();

                foreach($response as $responses) {

                    $item = json_decode($result,true);

                    $responsearray = array_merge($item['credentials'],$responsearray);

                }

                $token = $responsearray['token'];

                $siteIdToken = $responsearray['site']['id'];

                $userIdToken = $responsearray['user']['id'];

                echo '<b>Token :</b> '.$token.'<br>';

                echo '<b>siteIdToken :</b> '.$siteIdToken.'<br>';

                echo '<b>userIdToken :</b> '.$userIdToken.'<br>';

                $_SESSION['GTL_TableauToken']=$token;

                $_SESSION['GTL_TableauUserToken']=$userIdToken;

                $_SESSION['GTL_TableauSiteToken']=$siteIdToken;

                // echo $_SESSION['GTL_TableauToken'];

                }

                else

                {

                    echo 'Error';

                    header('location:../');

                }

            ?>

             

            Thanks

             

             

            Regards

            Mohammad Mossdiq Hasan

            • 3. Re: Tableau REST API: Using Javascript to get the Token
              Maciek La

              If you would like to interact with the REST API you would have to use server side scripting language as PHP or Python. This is due to X-AUTH token being stored in the HTTP cookie which is not accessible by the Browser-side languages such as Javascript.

              However, there is an undocumented API which can be access purely with JS and it provides vast range of REST API functionality. For step by step instructions you can find my post here:

               

              http://meowbi.com/2017/10/23/tableau-fields-definition-undocumented-api/