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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Obtain API Token with secure Authentication

Hello I'm new to Power BI,

 

I'm trying to obtain a third party REST API token using Web.Contents, but I want a secure way to do this other than entering a username and password into Power BI.

 

Saving the username/password to a secured file on a desktop or sharepoint are not ideal solutions.

 

Any advice?

 

let
Details="{""username""****":"",""password"":""****""}",
Token = Json.Document(Web.Contents("https://myurl/api/login",[Headers =[#"accept"="*/*",#"Company"="MyCompany",#"Content-Type"="application/json"], Content = Text.ToBinary(Details)]))
in
Token

 

Where **** for username and password are hidden and myurl is the url to login for my specific third party API.

2 REPLIES 2
Anonymous
Not applicable

Hi  @Anonymous,

Please review the following links, hope they can help you.

Using REST API:

How do I retrieve a bearer token from a service in Power BI?

Get token from Post API call

 

let

url = "https://login.windows.net/xxx/oauth2/v2.0/token",

headers = [#"Content-Type" = "application/x-www-form-urlencoded"],

postData = "grant_type=client_credentials&scope=xxx&client_id=xxx&client_secret=xxx",

response = Web.Contents(

url,

[Headers = headers,

Content = Text.ToBinary(postData)

]

),

JsonResponse=Json.Document(response),

access_token = JsonResponse[access_token]

in

access_token​

 

REST API Get Access Token

Using Power Automate:

How to use a service principal in Power BI Admin REST APIs in Power Automate

Best Regards

Anonymous
Not applicable

Thanks for your response @Anonymous,

This has partially helped me by exploring concepts for service as a principal. 
Enable service principal authentication for read-only admin APIs - Power BI | Microsoft Learn 

However, in the example it appears that the code is passing the client ID and client secret, this seems as unsecure as using a username and password.

 

I'm not sure if I should be attempting to access the token this way, or in another way described here: Handling authentication for Power Query connectors - Power Query | Microsoft Learn

 

Any advice is appreciated.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

July Power BI Update Carousel

Power BI Monthly Update - July 2026

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

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.