Don't miss your chance to take the Fabric Data Engineer (DP-600) exam for FREE! Find out how by attending the DP-600 session on April 23rd (pacific time), live or on-demand.
Learn moreNext up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now
I am completely new to APIs, PowerBI, and M (literally just found out the name for this programming language last week while frantically googling). I've managed to incorporate one public API into my dashboard project, but now the second data source requires a new authentication token daily. The site provided a python example on how to get the token, is there a way of re-writing it for the M language?
import requests
auth_url = 'https://api.citeline.com/token'
email = 'YOUR EMAIL'
password = 'YOUR PASSWORD'
def get_token():
data = {
'email': email,
'password': password
}
headers = {
"Content-Type": "application/json",
}
response = requests.request("POST", auth_url, json=data, headers=headers)
response_json = response.json()
access_token = 'Bearer {}'.format(response_json['access_token'])
return access_token
get_token()
Solved! Go to Solution.
@tiannie here is the M code for your scenario.
let
auth_url = "https://api.citeline.com/token",
email = "YOUR EMAIL",
password = "YOUR PASSWORD",
get_token = () =>
let
data = "{""email"": """ & email & """, ""password"": """ & password & """}",
headers = [
#"Content-Type" = "application/json"
],
// Make the HTTP request
response = Web.Contents(
auth_url,
[
Headers = headers,
Content = Text.ToBinary(data),
Timeout=#duration(0,0,30,0) // Set the timeout value as needed
]
),
// Parse the JSON response
response_json = Json.Document(response),
access_token = "Bearer " & response_json[access_token]
in
access_token
in
get_token()
Here's what this code does:
To use this code in Power BI, follow these steps:
@tiannie here is the M code for your scenario.
let
auth_url = "https://api.citeline.com/token",
email = "YOUR EMAIL",
password = "YOUR PASSWORD",
get_token = () =>
let
data = "{""email"": """ & email & """, ""password"": """ & password & """}",
headers = [
#"Content-Type" = "application/json"
],
// Make the HTTP request
response = Web.Contents(
auth_url,
[
Headers = headers,
Content = Text.ToBinary(data),
Timeout=#duration(0,0,30,0) // Set the timeout value as needed
]
),
// Parse the JSON response
response_json = Json.Document(response),
access_token = "Bearer " & response_json[access_token]
in
access_token
in
get_token()
Here's what this code does:
To use this code in Power BI, follow these steps:
Amazing, thank you so much!!!
@tiannie glad it helped you. 🙂
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.
| User | Count |
|---|---|
| 5 | |
| 3 | |
| 3 | |
| 3 | |
| 2 |
| User | Count |
|---|---|
| 7 | |
| 5 | |
| 5 | |
| 5 | |
| 4 |