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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

Generating Token Dynamically from Power BI Desktop

I am trying to generate REST API token dynamically from Power BI Desktop, using power Query.   I am not sure what is causing this error. Any help is appreciated.

An error occurred in the ‘’ query. DataSource.Error: Web.Contents failed to get contents from 'https://login.mypurecloud.com.au/oauth/token' (400): Bad Request
Details:
DataSourceKind=Web
DataSourcePath=https://login.mypurecloud.com.au/oauth/token
Url=https://login.mypurecloud.com.au/oauth/tokenCapture.PNG

1 ACCEPTED SOLUTION
Anonymous
Not applicable

After making few minor changes the code is generating token in Power BI . here is the modified code

let
GenerateToken = () =>
let
url = "https://login.mypurecloud.com.au/oauth/token",
body = "grant_type=client_credentials&client_id=<client-id>&client_secret=<Clinet_secret>",
headers = [#"Content-Type" = "application/x-www-form-urlencoded"],
response = Web.Contents(url, [Headers = headers, Content = Text.ToBinary(body)]),
jsonResponse = Json.Document(response),
accessToken = jsonResponse[access_token]
in
accessToken
in
GenerateToken

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

After making few minor changes the code is generating token in Power BI . here is the modified code

let
GenerateToken = () =>
let
url = "https://login.mypurecloud.com.au/oauth/token",
body = "grant_type=client_credentials&client_id=<client-id>&client_secret=<Clinet_secret>",
headers = [#"Content-Type" = "application/x-www-form-urlencoded"],
response = Web.Contents(url, [Headers = headers, Content = Text.ToBinary(body)]),
jsonResponse = Json.Document(response),
accessToken = jsonResponse[access_token]
in
accessToken
in
GenerateToken

Anonymous
Not applicable

Hi, 

Thanks for your support. i tried using above code , still gettig the same error. . Please see the attached screenshots.  in the second script i just tried displaying the token value instead passing the value to retrieve the data.

Postman is generating the token and retrieving the data for the same credentials

Capture_2.PNGCapture3.PNG

for the same URL and 

Anonymous
Not applicable

Hi @Anonymous ,

 

You can try the following code in general:

let
    GenerateToken = () =>
    let
        url = "https://login.mypurecloud.com.au/oauth/token",
        body = "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET",
        headers = [#"Content-Type" = "application/x-www-form-urlencoded"],
        response = Web.Contents(url, [Headers=headers, Content=Text.ToBinary(body)]),
        jsonResponse = Json.Document(response),
        accessToken = jsonResponse[access_token]
    in
        accessToken
in
    GenerateToken

let
    token = GenerateToken(),
    url = "https://api.yourservice.com/data",
    headers = [#"Authorization" = "Bearer " & token],
    response = Web.Contents(url, [Headers=headers]),
    data = Json.Document(response)
in
    data

 

Hope it helps!

 

Best regards,
Community Support Team_ Scott Chang

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

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.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.