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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Yggdrasill
Responsive Resident
Responsive Resident

Rest API with dynamic bearer token stopped working

Hello.

I'm using a PQ code to get an auth token to query data from REST API. It worked fine few weeks back but it doesn't work anymore.

I can get fetch data using Postman but PQ code (or something else) is broken and I don't know why.

Here's the code:

 

 

let

//Get Bearer token
//Note: Client_ID2 & Client_Secret2 are stored as parameters in Power Query
    baseURL = "MyWebpage",

    GetJson = Web.Contents(baseURL,
        [   RelativePath = "v1/auth",
            Headers =   [
                #"Accept"          = "*/*",
                #"Accept-Encoding" = "gzip, deflate",         
                #"Connection"      = "keep-alive",
                #"Content-Type"    = "application/x-www-form-urlencoded;charset=UTF-8"
                        ],
            Content = Text.ToBinary("client_id="&Client_ID2&"&client_secret="&Client_Secret2&"&grant_type=client_credentials")
        ]
    ),
    FormatAsJson = Json.Document(GetJson),
    access_token = FormatAsJson[data],
    token = access_token[token],   // Bearer Token

    //Get data using the Bearer Token

    Options = [
        RelativePath = "v1/company/employees",
        Headers         = [
            #"Authorization"   = "Bearer " & token,
            #"Accept"          = "application/json",
            #"Accept-Encoding" = "gzip, deflate",
            #"Connection"      = "keep-alive",
            #"Content-Type"    = "application/json"
    ],
        ManualStatusHandling = {400, 404}
    ],

    response = Web.Contents(baseURL, Options), // <- In this step I'm asked again for credentials 
    buffered = Binary.Buffer(response),
    response_metadata = Value.Metadata(response),
    status_code = response_metadata[Response.Status],
    final_result = if List.Contains({400, 404}, status_code) then response_metadata else buffered,
    from_json = Json.Document(final_result),
    data = from_json[data],
    #"Converted to Table" = Table.FromList(data, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
    #"Converted to Table

 

 

 

When I go to Data source settings I only have one data source there, as expected because I'm using RelativePath

 

Datasource = "MyWebpage"
Permissions is set to Anonymous but for some reason, when the code runs, it successfully returns a token everytime but then Power Query asks me again for credentials for the second query! This did not happen some few weeks back!

Now I want to connect again Anonymously since I'm using #"Authorization" but I get authentication error!

Yggdrasill_0-1634748284148.png

 

I also tried to create this scenario in Power BI Dataflows using Premium per user licence but again I'm asked twice to authenticate where first one runs smoothly (the auth part) but second fails!

Is this a problem with the October release of PBI Desktop? 

Kindest of regards

3 REPLIES 3
v-yingjl
Community Support
Community Support

Hi @Yggdrasill ,

As far as I know, there seems no known issue about it in Oct version of Power BI Desktop. Perhaps you can get back to the previous version of Power BI to check it again.

 

In addition, you can also try to clear the permission in Power BI and re-query it to get credentials to identify, or try to use custom functions to get dynamic token, refer:

Call Power BI Rest API with Dynamic Access Token From Power BI Desktop |Power BI Rest API | Part Two 

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-yingjl 

Thanks for the reply.

I've tried to downgrade my Desktop client to September 2021 but no success.

I've also cleared all permissions for the data source to re-enter the values I need there but also with no success.

 

I've also cleared all cache and Traces folder but also with no success.

For the latter part: I'm using the same method as descriped in the video but in one query which should work (and it did some weeks ago)

I'm going to try to copy the exact same way as described in the video and see if that works - but I doubt it will

Yggdrasill
Responsive Resident
Responsive Resident

Nobody?

Perhaps the PQ guru @ImkeF ?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors