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
Anonymous
Not applicable

Connecting to an API that uses OAuth with Power Query

Hi All,

 

I'm trying to ingest data from this API: https://api.nsw.gov.au/Product/Index/22

 

The site lets you test the endpoints in the browser and I get a valid response using the test credentials.

 

I'm able to get the access token in Power Query.  But the next request which uses the access token doesn't seem to work.  Power BI Desktop tells me the credentials used are not valid... 

 

M script below... any help would be much appreciated.

 

 

 

 

//https://api.nsw.gov.au/Product/Index/22
//Visit the site above and click on 'Sandbox and doc'
//This will present test credentials to try the endpoint

let
    testAPIKey = "1MYSRAx5yvqHUZc6VGtxix6oMA2qgfRT",
    testAPISecret = "BMvWacw15Et8uFGF",

    // Concatenates the test Key & Secret and converts to base64
    authKey = "Basic " & Binary.ToText(Text.ToBinary(testAPIKey & ":" & testAPISecret),0),

    url = "https://api.onegov.nsw.gov.au/oauth/client_credential/accesstoken",
    // Make a POST request to obtain a bearer token
    GetJson = Web.Contents(url,
        [
            Headers = 
                [
                #"Authorization"=authKey,
                #"Content-Type"="application/json"
                ],
            Content = Text.ToBinary("grant_type=client_credentials") 
        ]
    ),

    // Get the bearer token from the response
    AccessToken = Text.From(GetJson),

    GetJsonQuery = 
    Web.Contents("https://api.onegov.nsw.gov.au/FuelPriceCheck/v2/fuel/prices",
        [
        Headers=
            [
            #"Authorization"="Bearer " & AccessToken, 
            #"Content-Type"="application/json; charset=utf-8",
            #"apikey"=testAPIKey,
            #"Accept"="application/json",
            #"transactionid"="1",
            #"requesttimestamp"= DateTime.ToText(DateTime.LocalNow(), "dd/MM/yyyy hh:mm:ss tt")
            ]
        ]
    ),
    FormatAsJsonQuery = Json.Document(GetJsonQuery)
in
    FormatAsJsonQuery

 

 

 

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @Anonymous 

That NSW Govt website is way too hard to use.  It keeps giving me errors just trying to access it, like this

apierr.png

When I generate an access token on the website and then use it from the same website it tells me the access token is invalid.

 

Generate an Access Token

apires.png

 

Make an API Call

apicall.png

 

Check the Response

apiresp2.png

 

If the Access Token the site itself is generating is being rejected then there's not much hope.  I'm getting the same errors in Postman.

If I was you I'd be asking the website support what the issue is at their end.

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

5 REPLIES 5
UFP
New Member

Hi,

 

Allthough this thread is old. Thanks for the almost working example in this post.

I have replaced

AccessToken = Text.From(GetJson)

with

AccessToken = Json.Document(GetJson)[access_token]
 
Passing this AccessToken as the Bearer works for me.
 
Hope this helps others in the same situation,
Udo

 

PhilipTreacy
Super User
Super User

Hi @Anonymous 

That NSW Govt website is way too hard to use.  It keeps giving me errors just trying to access it, like this

apierr.png

When I generate an access token on the website and then use it from the same website it tells me the access token is invalid.

 

Generate an Access Token

apires.png

 

Make an API Call

apicall.png

 

Check the Response

apiresp2.png

 

If the Access Token the site itself is generating is being rejected then there's not much hope.  I'm getting the same errors in Postman.

If I was you I'd be asking the website support what the issue is at their end.

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Anonymous
Not applicable

Thank you very much for your help Philip.  I'll reach out to their support.

PhilipTreacy
Super User
Super User

Hi @Anonymous 

I'm getting the same error but I can't see anything wrong with your code.

I even signed up for an acount with the website and created myown key/secret and can get an Access Token OK, but auth to the actual API is failing.

I'll check it in Postman and see what is happening.

regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


amitchandak
Super User
Super User

@Anonymous , check if this can offer a help

https://www.myonlinetraininghub.com/connecting-to-an-oauth-api-like-paypal-with-power-query

https://docs.microsoft.com/en-us/power-query/handlingauthentication

https://community.powerbi.com/t5/Power-Query/OAuth2-REST-API-as-data-source/m-p/414748

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

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.