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

Try your skills in the Power BI Dataviz World Championship! Round one ends June 26. Join now

Reply
Anonymous
Not applicable

OAuth 2.0 Authorization Code flow grant type - Power Query API Connection

Hi, i'm tryng to to get Access Token from an OAuth2 Authorization Code flow.

Here the code: "PART 1" work fine and return the Authorization Code, bat "PART 2" don't work and return error.

 

Thanks for help

 

let

// PART 1: Get Authorization Code
Body =
"{
""username"":""<MyUsername>"",
""password"":""<MyPsw>"",
""scope"":[""<SelectedScope>""]
}",

url = "http://XXXXXXXXXXXXXXXXXXXXXXXXXXX/api/login/sso",
GetJson = Json.Document(Web.Contents(url, [Headers=[ContentType="application/json"], Content=Text.ToBinary(Body)])),
AuthorizationCode = GetJson,

// The previous code correctly returns the Authorization Code

// PART 2: Now I try to Get Access Token passing the Authorization Code, the client_id and the client_secret

// Base64 Encoding
a = "<client_id>" & ":" & "<client_secret>",
a2 = Text.ToBinary(a),
a3 = Binary.ToText(a2, BinaryEncoding.Base64),

TokenURL = "http://XXXXXXXXXXXXXXXXXXXXXXXXXXX/oauth2/token/",
Body2 =
"{
""grant_type"": ""authorization_code"",
""code"": """&AuthorizationCode&"""
}",

Source = Web.Contents(TokenURL, [Headers = [ContentType="application/json", Authorization = "Basic "&a3&""], Content = Text.ToBinary(Body2)])

in Source

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Solved, the problem was due to an erroneous Base64 Encoding

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Solved, the problem was due to an erroneous Base64 Encoding

Anonymous
Not applicable

Hi @Anonymous,

What type of error message displayed? Can you please share more detailed information?

How to Get Your Question Answered Quickly 

In addition, you can also try to add the 'relative path' option to your connector if it works:

let
    // PART 1: Get Authorization Code
    Body =
        "{'username':'<MyUsername>','password':'<MyPsw>','scope':['<SelectedScope>']}",
    url = "http://XXXXXXXXXXXXXXXXXXXXXXXXXXX/api/login/sso",
    GetJson =
        Json.Document(
            Web.Contents(
                url,
                [
                    Headers = [
                        ContentType = "application/json"
                    ],
                    Content = Text.ToBinary(Body)
                ]
            )
        ),
    AuthorizationCode = GetJson,
    // The previous code correctly returns the Authorization Code
    // PART 2: Now I try to Get Access Token passing the Authorization Code, the client_id and the client_secret
    // Base64 Encoding
    _client = "<client_id>:<client_secret>",
    TokenURL = "http://XXXXXXXX.XXXXXXXXXXX.com",
    Body2 =
        "{'grant_type': 'authorization_code','code': '"& AuthorizationCode& '" }",
    Source =
        Web.Contents(
            TokenURL,
            [
                Headers = [
                    ContentType = "application/json",
                    Authorization = "Basic " & Binary.ToText(Text.ToBinary(_client), BinaryEncoding.Base64)
                ],
                RelativePath="/oauth2/token/",
                Content = Text.ToBinary(Body2)
            ]
        )
in
    Source

Chris Webb's BI Blog: Using The RelativePath And Query Options With Web.Contents() In Power Query An...

Regards,
Xiaoxin Sheng

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

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.