Forum Discussion
Connect to Dynamics 365 on-premise using oAuth (client appid / secret /username / password)
Hi Community
I'm trying to connect Power BI to a on-premise Dynamics 365 Sales organisation. I need to authenticate using a Application user (works fine with my Kingsway Adapter and a developer friend of mine has coded a custom connector which also works fine). Is this possible sending all information in the oData URL og another smart way (so I don't have to rely on the custom connector)?
I have access to the following information but can't see how to authenticate with this when I connect to Dynamics 365 using either oData or the Dataverse connector?
{
"odata": "https://URL_A.com/api/data/v9.0/",
"token_url": "https://URL_B.com/adfs/oauth2/token",
"client_id": "some guid",
"client_secret": "some secret",
"username": "some ad username",
"password": "som ad password"
}
Please tell me if I can do this without a CUSTOM data connector (which does not work through the Gateway when publishing reports).
Thanks in advance for reading my post (please tell me if I need to move it to another location in this community)
/Henrik
WIKAP
1 Reply
- Divyaraj_Rathod
Helper II
let
GetToken = () =>
let
body = "grant_type=password"
& "&client_id=" & ClientId
& "&client_secret=" & ClientSecret
& "&username=" & Username
& "&password=" & Password
& "&resource=" & OdataUrl,
response = Json.Document(
Web.Contents(
TokenUrl,
[
Content = Text.ToBinary(body),
Headers = [#"Content-Type" = "application/x-www-form-urlencoded"]
]
)
)
in
response[access_token],
Source = Json.Document(
Web.Contents(
OdataUrl & "accounts",
[Headers = [Authorization = "Bearer " & GetToken()]]
)
)
in
Source
A few practical notes:
- Keep the base URL you register (OdataUrl) consistent everywhere - the string you pass as the first argument to the outer Web.Contents is what Power BI uses to map gateway credentials, so it needs to match exactly what you register in the gateway's data source settings.
- Since the token call and the data call are both just Web.Contents, this all runs as "Anonymous" from a Power BI gateway credential standpoint - there's no OAuth connector flow to configure in the Service, which is exactly what avoids the custom-connector/gateway limitation you ran into.
- You'll want to parameterize ClientId/ClientSecret/Username/Password as query parameters (or better, pull Username/Password from Data Source Settings via a Web.Contents credential rather than hardcoding), since embedding secrets directly in M isn't ideal for anything beyond quick testing.