4 Replies Latest reply on Aug 29, 2016 3:22 PM by dholdandnotused

    Refreshing extracts with undocumented API + Python

    Tom Cornford

      Could anyone provide any insight as to what is wrong with the refresh_extracts function?

       

      import math

      import xml.etree.ElementTree as ET

      import requests  # Contains methods used to make HTTP requests
      import sys

       

      from requests.packages.urllib3.fields import RequestField

      from requests.packages.urllib3.filepost import encode_multipart_formdata

       

      xmlns = {'t': 'http://tableau.com/api'}

       

      def sign_in(name, password, site=""):

         print('\n')

       

         url = SERVER + "/api/2.2/auth/signin"

         xml_payload_for_request = ET.Element('tsRequest')

        credentials_element = ET.SubElement(xml_payload_for_request, 'credentials', name=name, password=password)

        site_element = ET.SubElement(credentials_element, 'site', contentUrl=site)

        xml_payload_for_request = ET.tostring(xml_payload_for_request)

       

       

         server_response = requests.post("", data=xml_payload_for_request)

       


         if server_response.status_code != 200:

         print(server_response.text)

        sys.exit(1)

         xml_response = ET.fromstring(_encode_for_display(server_response.text))

       

         token = xml_response.find('t:credentials', namespaces=xmlns).attrib.get('token')

        site_id = xml_response.find('.//t:site', namespaces=xmlns).attrib.get('id')

        user_id = xml_response.find('.//t:user', namespaces=xmlns).attrib.get('id')

         return token, site_id, user_id

       

       

      def _encode_for_display(text):

         return text.encode('ascii', errors="backslashreplace").decode('utf-8')

       

      def sign_out():

       

         global TOKEN

        url = SERVER + "/api/2.2/auth/signout"
         server_response = requests.post(url, headers={'x-tableau-auth': TOKEN})

        TOKEN = None
         return

      def refresh_datasources():

       

        url = SERVER + "/vizportal/api/web/v1/runExtractRefreshesOnDatasources"

         datasourceIds = '38e2355d-faef-4e7c-9bfd-eb9bb5521178'


         method = "runExtractRefreshesOnDatasources"
         type = "RefreshExtract"
         xml_payload_for_request = ET.Element('tsRequest')

        request_element = ET.SubElement(xml_payload_for_request,'request_payload', method=method)

         request_element2 = ET.SubElement(request_element, 'params',ids=datasourceIds, type="RefreshExtract")

        xml_payload_for_request = ET.tostring(xml_payload_for_request)

       

       

        server_response = requests.post(url, data=xml_payload_for_request,headers={'x-tableau-auth': TOKEN, 'content-type': 'text/xml'})

       

         print(server_response)

         print(server_response.text)

       

         return

      if __name__ == '__main__':

         ####
        # Global variables -- SET THESE BEFORE RUNNING
        ####
         SERVER = ""  # Set to the server URL without a trailing slash (/).
         USER = ''  # Set to your Tableau Server username. The user must be a server administrator.
         PASSWORD = ''  # Set to your Tableau Server password.
         WORKBOOK_NAME = ''  # Set to the name of the workbook to publish. Include a path if necessary.
         CHUNKED = True

         print("Signing in")

        TOKEN, SITE_ID, MY_USER_ID = sign_in(USER, PASSWORD)

       

        refresh_datasources()

       

        sign_out()

         print("\nSigned out, and the authentication token has been invalidated")