The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.
Hi,
I have an POST API call that sends an email, in the content of that email is a link containing a direct login link with a JWT (app.domain.com/auth/login/code/bWFyY3VzLnJvc2tpbGxA.......) , and also a 6 digit aphanumeric code that is used by the app to create a bearer token (api.domain.com/v0/auth/code) taking the users email address and the 6 digit code as body data
How can I feed the 6 digit code or the JWT back to the query
Using = Json.Document(Web.Contents(URL2,[Headers=[#"Content-Type"="application/json; charset=utf-8"],Content=Text.ToBinary(Body2)])) I get a 500 error, so i modified it to use a Post action
= Json.Document(WebAction.Request(WebMethod.Post,URL2,[Headers=[#"Content-Type"="application/json; charset=utf-8"],Content=Text.ToBinary(Body2)]))
but I get "Expression.Error: We haven't been given permission to perform actions against this resource."
is there a better way to do this, supported by PowerBI?
Thanks
Solved! Go to Solution.
Hi @MarcusR1 ,
Please try below steps:
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:
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": "user@example.com"}
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": "user@example.com"}
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
Please mark this as solution if it helps you. Appreciate Kudos.
Hi @MarcusR1 ,
Your issue seems to be related to authentication and Power BI service limitations when calling REST APIs.
Power BI does not support direct API calls for authentication-sensitive actions, so the best way is to:
If you must use Power Query (Web.Contents), try structuring your API request correctly:
let
URL = "https://api.domain.com/v0/auth/code",
Body = "{""email"":""user@example.com"", ""code"":""123ABC""}",
Response = Json.Document(
Web.Contents(URL,
[
Headers = [#"Content-Type"="application/json"],
Content = Text.ToBinary(Body)
]))
in
Response
If the API call requires a user login session or secrets that Power BI doesn’t support directly, Power Automate or an external script (Azure Function, Python, etc.) is the best way to pass the JWT and 6-digit code securely.
Please mark this as solution if it helps you. Appreciate kudos.
Hi @FarhanJeelani
Thank you for your detailed response, it is disappointing that PowerBI has so many basic things that it can't do. I am trying to create a template for distribution to non-technical clients so that they can connect to our API and customize their own reports, so forcing clients to download Power Automate and fiddle around with configuration settings is not a suitable solution. Can you help me with the Python script, presumably it will be in Power Query?
Hi @MarcusR1 ,
Please try below steps:
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:
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": "user@example.com"}
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": "user@example.com"}
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
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.
User | Count |
---|---|
65 | |
60 | |
55 | |
54 | |
31 |
User | Count |
---|---|
180 | |
88 | |
70 | |
46 | |
46 |