<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Pulling Data from Qlik Cloud through python on window machine in Integration, Extension &amp; APIs</title>
    <link>https://community.qlik.com/t5/Integration-Extension-APIs/Pulling-Data-from-Qlik-Cloud-through-python-on-window-machine/m-p/2471562#M20956</link>
    <description>&lt;P&gt;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/141812"&gt;@jprdonnelly&lt;/a&gt;&amp;nbsp; Thanks,&lt;/P&gt;
&lt;P&gt;I converted the C# example to python but it was giving the following errors:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;NameError                                 Traceback (most recent call last)
Cell In[8], line 61
     58     return json.dumps(body)
     60 if __name__ == "__main__":
---&amp;gt; 61     main()

Cell In[8], line 14, in main()
     11 output_file = "output.xlsx"
     13 # Initialize RestClient
---&amp;gt; 14 client = RestClient(url)
     15 client.as_api_key_via_qcs(api_key)
     17 # Create request body

NameError: name 'RestClient' is not defined&lt;/LI-CODE&gt;
&lt;P&gt;here is my code:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import requests
import json
import time
# import RestClients

def main():
    url = "https://tenant.eu.qlikcloud.com"
    api_key = "api key"
    object_id = "ZgGPbV"
    output_file = "output.xlsx"

    # Initialize RestClient
    client = RestClient(url)
    client.as_api_key_via_qcs(api_key)

    # Create request body
    request_body = create_request_body(app_id, object_id)

    # Post request to initiate report generation
    http_rsp = client.post_http_async("/api/v1/reports", request_body)
    http_result = http_rsp.result()
    status_location = http_result.headers["Location"]

    print("Report generation requested. Awaiting process to complete...")
    data_location = await_export_completion(client, status_location)

    print("Report generation completed. Downloading exported file...")
data_location_uri = urljoin(url, data_location)
    bytes_data = client.get_bytes(data_location_uri)

    with open(output_file, "wb") as f:
        f.write(bytes_data)

    print(f"Wrote {len(bytes_data)} bytes to file: {output_file}")

def await_export_completion(client, status_location):
    rsp = client.get_json(status_location)
    while rsp["status"] != "done":
        print("    Current status:", rsp["status"])
        time.sleep(1)
        rsp = client.get_json(status_location)
    print("    Current status:", rsp["status"])
    return rsp["results"][0]["location"]

def create_request_body(app_id, object_id):
    body = {
        "type": "sense-data-1.0",
        "output": {
            "outputId": "Chart_excel",
            "type": "xlsx"
        },
        "senseDataTemplate": {
            "appId": app_id,
            "id": object_id
        }
    }
   return json.dumps(body)

if __name__ == "__main__":
    main()
&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 18 Jul 2024 17:02:01 GMT</pubDate>
    <dc:creator>olaoyesunday1</dc:creator>
    <dc:date>2024-07-18T17:02:01Z</dc:date>
    <item>
      <title>Pulling Data from Qlik Cloud through python on window machine</title>
      <link>https://community.qlik.com/t5/Integration-Extension-APIs/Pulling-Data-from-Qlik-Cloud-through-python-on-window-machine/m-p/2471259#M20944</link>
      <description>&lt;P&gt;Hello folks,&lt;/P&gt;
&lt;P&gt;I am trying to read data from Qlik Cloud into my Python script on my Windows laptop, but I'm encountering errors:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import requests
import pandas as pd
import matplotlib.pyplot as plt
import base64
import json
qlik_base_url = "https://mytenant.qlikcloud.com/"
qlik_app_id = "f5555629-bb12-4983-854b-1ed7c9682f55"
qlik_table_object_id = "ZgGPbV"
qlik_api_token = "api_key" #this api key is correct

qlik_data_url = f"{qlik_base_url}/api/v1/apps/{qlik_app_id}/tables/{qlik_table_object_id}/data" 
headers = { "Authorization": "Bearer " + qlik_api_token } 
response = requests.get(qlik_data_url, headers=headers)
# Check for successful response status
if response.status_code == 200:
    try:
        table_data = response.json()
    except requests.exceptions.JSONDecodeError as e:
        print(f"Error decoding JSON: {e}")
        table_data = None
else:
    print(f"Failed to fetch data. Status code: {response.status_code}")
    table_data = None&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but the output is&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Failed to fetch data. Status code: 401&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;please, can anyone help me to resolve this, I am using correct api_key, my url, appId and object Id are all correct&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2024 16:05:15 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Integration-Extension-APIs/Pulling-Data-from-Qlik-Cloud-through-python-on-window-machine/m-p/2471259#M20944</guid>
      <dc:creator>olaoyesunday1</dc:creator>
      <dc:date>2024-07-17T16:05:15Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Data from Qlik Cloud through python on window machine</title>
      <link>https://community.qlik.com/t5/Integration-Extension-APIs/Pulling-Data-from-Qlik-Cloud-through-python-on-window-machine/m-p/2471532#M20954</link>
      <description>&lt;P&gt;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/261763"&gt;@olaoyesunday1&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Please investigate the following links:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="https://qlik.dev/apis/rest/reports/" target="_blank" rel="noopener"&gt;Reports API (REST)&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="https://github.com/kolsrud/qlik_rest_sdk/blob/master/Qlik.Sense.RestClient/Examples/QcsExportExcel/Program.cs" target="_blank" rel="noopener"&gt;Example in C#&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The Reporting API off-loads the export/PDF generation workload from the Engine in Qlik Cloud. You will be able to achieve the same outcome, it will just require a different approach.&lt;/P&gt;
&lt;P&gt;Cheers!&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2024 14:40:08 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Integration-Extension-APIs/Pulling-Data-from-Qlik-Cloud-through-python-on-window-machine/m-p/2471532#M20954</guid>
      <dc:creator>jprdonnelly</dc:creator>
      <dc:date>2024-07-18T14:40:08Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Data from Qlik Cloud through python on window machine</title>
      <link>https://community.qlik.com/t5/Integration-Extension-APIs/Pulling-Data-from-Qlik-Cloud-through-python-on-window-machine/m-p/2471562#M20956</link>
      <description>&lt;P&gt;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/141812"&gt;@jprdonnelly&lt;/a&gt;&amp;nbsp; Thanks,&lt;/P&gt;
&lt;P&gt;I converted the C# example to python but it was giving the following errors:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;NameError                                 Traceback (most recent call last)
Cell In[8], line 61
     58     return json.dumps(body)
     60 if __name__ == "__main__":
---&amp;gt; 61     main()

Cell In[8], line 14, in main()
     11 output_file = "output.xlsx"
     13 # Initialize RestClient
---&amp;gt; 14 client = RestClient(url)
     15 client.as_api_key_via_qcs(api_key)
     17 # Create request body

NameError: name 'RestClient' is not defined&lt;/LI-CODE&gt;
&lt;P&gt;here is my code:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import requests
import json
import time
# import RestClients

def main():
    url = "https://tenant.eu.qlikcloud.com"
    api_key = "api key"
    object_id = "ZgGPbV"
    output_file = "output.xlsx"

    # Initialize RestClient
    client = RestClient(url)
    client.as_api_key_via_qcs(api_key)

    # Create request body
    request_body = create_request_body(app_id, object_id)

    # Post request to initiate report generation
    http_rsp = client.post_http_async("/api/v1/reports", request_body)
    http_result = http_rsp.result()
    status_location = http_result.headers["Location"]

    print("Report generation requested. Awaiting process to complete...")
    data_location = await_export_completion(client, status_location)

    print("Report generation completed. Downloading exported file...")
data_location_uri = urljoin(url, data_location)
    bytes_data = client.get_bytes(data_location_uri)

    with open(output_file, "wb") as f:
        f.write(bytes_data)

    print(f"Wrote {len(bytes_data)} bytes to file: {output_file}")

def await_export_completion(client, status_location):
    rsp = client.get_json(status_location)
    while rsp["status"] != "done":
        print("    Current status:", rsp["status"])
        time.sleep(1)
        rsp = client.get_json(status_location)
    print("    Current status:", rsp["status"])
    return rsp["results"][0]["location"]

def create_request_body(app_id, object_id):
    body = {
        "type": "sense-data-1.0",
        "output": {
            "outputId": "Chart_excel",
            "type": "xlsx"
        },
        "senseDataTemplate": {
            "appId": app_id,
            "id": object_id
        }
    }
   return json.dumps(body)

if __name__ == "__main__":
    main()
&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 18 Jul 2024 17:02:01 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Integration-Extension-APIs/Pulling-Data-from-Qlik-Cloud-through-python-on-window-machine/m-p/2471562#M20956</guid>
      <dc:creator>olaoyesunday1</dc:creator>
      <dc:date>2024-07-18T17:02:01Z</dc:date>
    </item>
  </channel>
</rss>

