Forum Discussion
REST API with secret email key
- 1 year ago
Hi MarcusR1 ,
Please try below steps:
- Send the POST request to retrieve the JWT and 6-digit code.
- Use the JWT or code to authenticate the user.
- Return the API response as a dataframe.
Steps to Implement:
-
Enable Python in Power BI
Go to File > Options and settings > Options > Python scripting and set up your Python environment. -
Write the Python Script in Power Query:
- Go to Home > Transform Data > Transform > Run Python Script and paste the following:
import requests import pandas as pd # Step 1: Get JWT and 6-digit code auth_url = "https://api.domain.com/v0/auth/code" headers = {"Content-Type": "application/json"} payload = {"email": "[email protected]"} auth_response = requests.post(auth_url, json=payload, headers=headers) if auth_response.status_code == 200: auth_data = auth_response.json() jwt_token = auth_data.get("jwt") # Assuming JWT is returned auth_code = auth_data.get("code") # Assuming 6-digit code is returned # Step 2: Use JWT or code to authenticate api_url = "https://api.domain.com/v0/protected_endpoint" headers["Authorization"] = f"Bearer {jwt_token}" payload = {"code": auth_code, "email": "[email protected]"} api_response = requests.post(api_url, json=payload, headers=headers) if api_response.status_code == 200: data = api_response.json() df = pd.DataFrame(data) # Convert to DataFrame for Power BI else: df = pd.DataFrame([{"error": "Failed to authenticate"}]) else: df = pd.DataFrame([{"error": "Failed to fetch auth data"}]) df
- Go to Home > Transform Data > Transform > Run Python Script and paste the following:
- Load Data in Power BI
- After running the script, Power BI will convert the output to a table.
- Click Close & Apply to load the data into your report.
Please mark this as solution if it helps you. Appreciate Kudos.
Hi MarcusR1 ,
Please try below steps:
- Send the POST request to retrieve the JWT and 6-digit code.
- Use the JWT or code to authenticate the user.
- Return the API response as a dataframe.
Steps to Implement:
-
Enable Python in Power BI
Go to File > Options and settings > Options > Python scripting and set up your Python environment. -
Write the Python Script in Power Query:
- Go to Home > Transform Data > Transform > Run Python Script and paste the following:
import requests import pandas as pd # Step 1: Get JWT and 6-digit code auth_url = "https://api.domain.com/v0/auth/code" headers = {"Content-Type": "application/json"} payload = {"email": "[email protected]"} auth_response = requests.post(auth_url, json=payload, headers=headers) if auth_response.status_code == 200: auth_data = auth_response.json() jwt_token = auth_data.get("jwt") # Assuming JWT is returned auth_code = auth_data.get("code") # Assuming 6-digit code is returned # Step 2: Use JWT or code to authenticate api_url = "https://api.domain.com/v0/protected_endpoint" headers["Authorization"] = f"Bearer {jwt_token}" payload = {"code": auth_code, "email": "[email protected]"} api_response = requests.post(api_url, json=payload, headers=headers) if api_response.status_code == 200: data = api_response.json() df = pd.DataFrame(data) # Convert to DataFrame for Power BI else: df = pd.DataFrame([{"error": "Failed to authenticate"}]) else: df = pd.DataFrame([{"error": "Failed to fetch auth data"}]) df
- Go to Home > Transform Data > Transform > Run Python Script and paste the following:
- Load Data in Power BI
- After running the script, Power BI will convert the output to a table.
- Click Close & Apply to load the data into your report.
Please mark this as solution if it helps you. Appreciate Kudos.
Hi FarhanJeelani ,
Thank you again for your detailed suggestion, this is really complicated though!
The script doesn't work because the Authentication code is sent by email (which is completely disconnected from the script, so there is no way to pass it back in), and I can't even save the Code as a variable in Power Query because the Python script doesn't seem to recognize PQ variables. More fundamentally than that, though, is that this requires the user to install Python (and requests libraries) which is far too technical for a Power BI user who is used to just pulling in data using saved credentials (Basic Auth) or SSO - also, their IT department won't want them installing Python on their PCs. Surely, with the world moving quickly towards email verification (for security reasons) Power BI should be moving with the times and developing functions/settings that can cope with this? Thanks for trying.