Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
olaoyesunday1
Contributor III
Contributor III

Detailed python scripts to Link Qlik Cloud.

Hello Folks, 

 I was trying to generate detailed python scripts to read data from a qlik cloud table, output it as a pdf file on my laptop

 

I keep on encountering  error 401. i,e Failed to fetch data. Status code: 401.

here is my code:

    
import requests
import pandas as pd
import matplotlib.pyplot as plt
import base64

# Qlik Sense Configuration
qlik_app_id = "d0c200c8-b3bb-4157-81b7-4d18d44856a3"
qlik_table_object_id = "tNfNKkC"

# Authentication credentials (replace with your actual credentials)
qlik_username = "my username"
qlik_password = "password"

# Fetch data from Qlik Sense with authentication headers
qlik_data_url = f"{qlik_base_url}/api/v1/apps/{qlik_app_id}/tables/{qlik_table_object_id}/data"
headers = {
    "Authorization": "Basic " + base64.b64encode(f"{qlik_username}:{qlik_password}".encode()).decode()
}
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

# Continue with the rest of the script...

# Convert Qlik table data to a Pandas DataFrame
df = pd.DataFrame(table_data)

# Data processing or analysis (replace this with your specific requirements)
# For this example, let's just plot the data and save it as a PDF
plt.figure(figsize=(10, 6))
plt.bar(df['Customer'], df['Sum(Sales)'])
plt.xlabel('Customer')
plt.ylabel('Sum(Sales)')
plt.title('Top Customers by Sales in Qlik Sense')

# Save the plot as a PDF file
pdf_file_path = "top_customers.pdf"
plt.savefig(pdf_file_path, format='pdf')

# Display or further process the DataFrame as needed
print(df.head())
    
Please, what can I do that I have not done in python.
Labels (1)
1 Solution

Accepted Solutions
olaoyesunday1
Contributor III
Contributor III
Author

Thanks, 

I have gotten the solutions,

  (1)  I need Api_key.

 (2) I used pytoqlik library.  

p2q = pytoqlik.Pytoqlik(api_key=key, tenant=url, appId=ID)

View solution in original post

3 Replies
Scotchy
Partner - Creator
Partner - Creator

Hey there, 

Possibly try this... 

 
The error 401 you are encountering, "Failed to fetch data. Status code: 401," typically indicates an authentication issue. This means that the server you're trying to access is rejecting your request because it doesn't recognize your credentials as valid. In your case, this is happening when you're trying to access data from a Qlik Cloud table.

Here are a few steps you can take to troubleshoot and resolve this issue:

  1. Verify Credentials: Ensure that your qlik_username and qlik_password are correct. These should be the credentials you use to log into the Qlik Cloud service.

  2. Authentication Method: Qlik Cloud might not support basic authentication (username and password) in the way you've implemented it. Usually, Qlik Cloud uses API tokens for authentication. You'll need to generate an API token from your Qlik Cloud account and use that for authentication instead of username and password.

  3. API Token Usage: If you are using an API token, the header format is usually "Authorization": "Bearer <your_api_token>". Make sure you're using the correct format.

  4. API Endpoint: Ensure that the URL you are using to fetch data is correct. Verify the base URL and the specific endpoint for fetching table data.

  5. API Permissions: Make sure that your account or API token has the necessary permissions to access the data you're requesting.

  6. API Documentation: Consult the Qlik Cloud API documentation for specific details on how to authenticate and fetch data. There might be specific steps or parameters required.

Here's an example of how you might modify the authentication part of your script if you were using an API token:

python
# Qlik Sense Configuration qlik_base_url = "https://ioco2.eu.qlikcloud.com/" qlik_app_id = "d0c200c8-b3bb-4157-81b7-4d18d44856a3" qlik_table_object_id = "tNfNKkC" # API Token (replace with your actual API token) qlik_api_token = "your_api_token_here" # Fetch data from Qlik Sense with Bearer Token for authentication 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) # ... rest of your script

Remember to replace "your_api_token_here" with your actual API token. If you need help generating an API token in Qlik Cloud, you should consult their documentation or support resources.

olaoyesunday1
Contributor III
Contributor III
Author

Thanks, 

I have gotten the solutions,

  (1)  I need Api_key.

 (2) I used pytoqlik library.  

p2q = pytoqlik.Pytoqlik(api_key=key, tenant=url, appId=ID)

olaoyesunday1
Contributor III
Contributor III
Author

@Scotchy  I tried your approach it did not work. I am using window computer. here is what I did : 

# Hello Qlik Cloud Python Program
import requests
import pandas as pd
import matplotlib.pyplot as plt
import base64
import json

# Qlik Sense Configuration
qlik_base_url = "https://mytenant.eu.qlikcloud.com/" # I do not want to show my real tenant on public
qlik_app_id = "f5555629-bb12-4983-854b-1ed7c9682f55"
qlik_table_object_id = "ZgGPbV"
qlik_api_token = "I used correct api key here"

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

the output was 

Failed to fetch data. Status code: 401