Forum Discussion
Connect API oauth2 to pull data
- 4 years ago
Falcon
I've just noticed that the json was malformed + I've added a relative path for the auth.
Try this:let ClientId = "XXXXXXX", ClientSecret = "XXXXXXX", Uri = "https://secure9.aladtec.com/", Body ="{ ""grant_type"": ""client_credentials"", ""client_id"": """ & ClientId & """, ""client_secret"": """ & ClientSecret & """ }", OAuth2Response = Web.Contents(Uri, [RelativePath = "mohcacc/api/v2/oauth/token", Content=Text.ToBinary(Body)]) in OAuth2Response
Hi Falcon,
I'll need to see your actual code to figure it out.
Instead, have a look at the following code.
It will generate an OAuth2 access token from AAD, to the Power BI Service, using a service principal.
You probably need an access token to another scope or not to AAD at all...
let
TenantId = "XXXXXX",
ClientId = "XXXXXX",
ClientSecret = "XXXXXX",
Uri = "https://login.microsoftonline.com/" & TenantId & "/oauth2/v2.0/token/",
Body =
"scope=https://analysis.windows.net/powerbi/api/.default" &
"&grant_type=client_credentials" &
"&client_id=" & ClientId &
"&client_secret=" & ClientSecret,
OAuth2Response = Json.Document(Web.Contents(Uri, [Content=Text.ToBinary(Body)])),
access_token = OAuth2Response[access_token]
in
access_token
Hello Sparta Bi,
Here is my code by following the above instruction. The "tokenUserName" and "tokenPassword" hold "client_id" and "client_secret" respectively. The "tokenAddress" holds the URL which is "https://xxxxxxxxxxx/mohcacc/api/v2/oauth/token". I don't have TenantId and scope as in your example. How do I get them?
let body = "{
""username"" = " & tokenUserName & ",
""password"" = " & tokenPassword & "
}",
getToken = Web.Contents(
tokenAddress,
[Content = Text.ToBinary(body)]
)
in getToken