Forum Discussion
Dyanmic access token for API
- 2 years ago
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:
- It defines the auth_url, email, and password variables.
- It defines a function called get_token that performs the API call to retrieve the authentication token.
- Inside the get_token function:
- It constructs the request body (data) in JSON format.
- It defines the request headers.
- It makes an HTTP POST request to the auth_url with the provided email and password.
- It parses the JSON response to extract the access_token.
- It returns the access_token.
To use this code in Power BI, follow these steps:
- Open Power BI Desktop.
- Go to "Home" > "Transform Data" to open Power Query Editor.
- In Power Query Editor, go to "Home" > "New Source" > "Blank Query".
- Copy and paste the provided M code into the formula bar.
- Replace "YOUR EMAIL" and "YOUR PASSWORD" with your actual email and password.
- Click "Done" to save the query.
- Close and apply the changes.
- Use the get_token function as needed in your Power BI report to retrieve the authentication token.
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!
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:
- It defines the auth_url, email, and password variables.
- It defines a function called get_token that performs the API call to retrieve the authentication token.
- Inside the get_token function:
- It constructs the request body (data) in JSON format.
- It defines the request headers.
- It makes an HTTP POST request to the auth_url with the provided email and password.
- It parses the JSON response to extract the access_token.
- It returns the access_token.
To use this code in Power BI, follow these steps:
- Open Power BI Desktop.
- Go to "Home" > "Transform Data" to open Power Query Editor.
- In Power Query Editor, go to "Home" > "New Source" > "Blank Query".
- Copy and paste the provided M code into the formula bar.
- Replace "YOUR EMAIL" and "YOUR PASSWORD" with your actual email and password.
- Click "Done" to save the query.
- Close and apply the changes.
- Use the get_token function as needed in your Power BI report to retrieve the authentication token.
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!
Amazing, thank you so much!!!
- johnbasha332 years agoSuper User
tiannie glad it helped you. 🙂
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!