Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
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
Solved! Go to Solution.
Solved, the problem was due to an erroneous Base64 Encoding
Solved, the problem was due to an erroneous Base64 Encoding
Hi @Marco_Iac,
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
Regards,
Xiaoxin Sheng
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
8 | |
6 | |
5 | |
2 | |
2 |
User | Count |
---|---|
4 | |
4 | |
3 | |
3 | |
2 |