Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
tiannie
Regular Visitor

Dyanmic access token for API

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()             

 

 

1 ACCEPTED SOLUTION
johnbasha33
Super User
Super User

@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:

  1. It defines the auth_url, email, and password variables.
  2. It defines a function called get_token that performs the API call to retrieve the authentication token.
  3. 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:

  1. Open Power BI Desktop.
  2. Go to "Home" > "Transform Data" to open Power Query Editor.
  3. In Power Query Editor, go to "Home" > "New Source" > "Blank Query".
  4. Copy and paste the provided M code into the formula bar.
  5. Replace "YOUR EMAIL" and "YOUR PASSWORD" with your actual email and password.
  6. Click "Done" to save the query.
  7. Close and apply the changes.
  8. 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 !!



View solution in original post

3 REPLIES 3
johnbasha33
Super User
Super User

@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:

  1. It defines the auth_url, email, and password variables.
  2. It defines a function called get_token that performs the API call to retrieve the authentication token.
  3. 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:

  1. Open Power BI Desktop.
  2. Go to "Home" > "Transform Data" to open Power Query Editor.
  3. In Power Query Editor, go to "Home" > "New Source" > "Blank Query".
  4. Copy and paste the provided M code into the formula bar.
  5. Replace "YOUR EMAIL" and "YOUR PASSWORD" with your actual email and password.
  6. Click "Done" to save the query.
  7. Close and apply the changes.
  8. 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!!!

@tiannie glad it helped you. 🙂

Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!


Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.