I’m trying to call an API from Power BI using Power Query M, but I’m encountering issues retrieving the expected publicToken. Here are the details of my situation:
What I’ve Tried: Constructed the API URL correctly with the master ID. Used the API key and secret for authentication. Attempted to extract the publicToken from the response JSON
Current Code:
MasterID = [master.id],
apiKey = "1234",
apiKeySecret = "12345",
url = "https://a.blazemeter.com/api/v4/masters/" & Text.From(MasterID) & "/public-token?api_key=" & apiKey & "&api_key_secret=" & apiKeySecret,
response = Web.Contents(url),[
Content=Text.ToBinary("")
])),
Source = Json.Document(response),
publicToken = try Source[result][publicToken] otherwise null
in
publicToken```
**Error Message:**
I received the following error:
DataSource.Error: Web.Contents with the Content option is only supported when connecting anonymously.
Details:
DataSourceKind=Web
The error you’re encountering in Power Query (Web.Contents with the Content option is only supported when connecting anonymously) suggests that Power BI is attempting to authenticate your API request, but it’s using a method that conflicts with the approach you’re trying to use.
Here are a few key points to address and a refined approach to help solve this:
Steps to Fix:
Authentication Method: Power BI doesn’t support passing authentication data (like API key and secret) via the URL query string directly when using Web.Contents with a Content option. Instead, you should use headers for authentication or the proper authentication mechanism provided by Power BI.
Web.Contents Usage: When using Web.Contents, it’s recommended to pass authentication credentials either via headers (for API key-based authentication) or a suitable authentication method (like OAuth if supported). Power BI will handle authentication more securely.
Revised Approach for API Key Authentication:
If your API uses an API key and secret for authentication (and not OAuth), you’ll need to pass these credentials via the HTTP headers, not the URL.
You can do this by adding a Headers option to your Web.Contents call. Here’s how you can modify your code:
Revised Code:
let
MasterID = [master.id],
apiKey = "1234",
apiKeySecret = "12345",
url = "https://a.blazemeter.com/api/v4/masters/" & Text.From(MasterID) & "/public-token",
// Using Web.Contents with Header for API authentication
response = Web.Contents(url,
[
Headers = [
#"api_key" = apiKey,
#"api_key_secret" = apiKeySecret
]
]),
// Convert response to JSON
Source = Json.Document(response),
// Try to extract publicToken safely
publicToken = try Source[result][publicToken] otherwise null
in
publicToken
Explanation:
API Key in Headers: The api_key and api_key_secret are passed in the Headers section instead of the URL query string.
Web.Contents without Content: You’ve removed the unnecessary Content=Text.ToBinary("") part since you aren’t sending a body payload with this GET request.
Error Handling: The try block ensures you won’t get an error if the publicToken isn’t found.
Debugging Tips:
Check the API Documentation: Ensure that the API you’re calling supports key-based authentication via headers.
Check Response: If you keep having issues, try to output the full response in Power Query by replacing the publicToken extraction with just Source, which will show the raw JSON response.
let
MasterID = [master.id],
apiKey = "1234",
apiKeySecret = "12345",
url = "https://a.blazemeter.com/api/v4/masters/" & Text.From(MasterID) & "/public-token",
// Using Web.Contents with Header for API authentication
response = Web.Contents(url,
[
Headers = [
#"api_key" = apiKey,
#"api_key_secret" = apiKeySecret
]
]),
// Convert response to JSON
Source = Json.Document(response)
in
Source
This will help you inspect the actual response and verify if you are receiving the correct data from the API.
Additional Considerations:
Authentication Method: If your API uses a more complex method (like OAuth), you’ll need to configure Power BI to use OAuth for the API connection.
Error Handling: Be sure to handle possible errors (like 404, 403, etc.) in your queries to avoid disruptions in your Power BI workflow.
Let me know if this resolves the issue or if you’re encountering a different response after this change!