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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
Anonymous
Not applicable

Schedule refresh with OAuth2 authentication for Grant type "Client Credentials"

Hi All,

We are creating custom connector in visual studio using power query to get data from odata API.

 

Our API is using oAuth2.0 - 

  grant_type = "client_credentials",

 Does anyone have any idea how to implement it, any help/lead will be appreciated.

 

We have taken help from this blog -

Source Link

 

[DataSource.Kind="p2", Publish="p2.Publish"]
shared p2.Contents = () =>
let
output = gettoken()

in
output;




//Query Record Parameter
query = [
clientId = client_id,
clientSecret = client_secret,
grantType = "client_credentials",
scope="all"
];

gettoken = () =>
let
oauthRequest = Json.FromValue(query),
Response = Web.Contents(token_url, [
Content = oauthRequest,
Headers=[#"Content-type" = "application/json",#"Accept" = "application/json"], ManualStatusHandling = {400}
]
),
Parts = Json.Document(Response),
authToken = if (Record.HasFields(Parts, {"error", "error_description"})) then
error Error.Record(Parts[error], Parts[error_description], Parts)
else
"Bearer " & Parts[access_token],

Source=OData.Feed(odata_url,[#"Authorization" = authToken],null),
//end of do not edit section
User_View = Source{[Name="vw_rpt_user",Signature="table"]}[Data]
in
User_View;

// Data Source Kind description
p2 = [
TestConnection = (dataSourcePath) => { "p2.Contents", dataSourcePath },
Authentication = [
OAuth = [
StartLogin=StartLogin,
FinishLogin=FinishLogin
]
],
Label = Extension.LoadString("DataSourceLabel")
];

StartLogin = (resourceUrl, state, display) =>
let
authorizeUrl = token_url,
redirect_uri = odata_url
in
[
LoginUri = authorizeUrl,
CallbackUri = redirect_uri,
WindowHeight = 720,
WindowWidth = 1024,
Context = null
];

FinishLogin = (context, callbackUri, state) =>
let
// parse the full callbackUri, and extract the Query string
parts = Uri.Parts(callbackUri)[Query],
// if the query string contains an "error" field, raise an error
// otherwise call TokenMethod to exchange our code for an access_token
result = if (Record.HasFields(parts, {"error", "error_description"})) then
error Error.Record(parts[error], parts[error_description], parts)
else
gettoken()
in
result;

// Data Source UI publishing description
p2.Publish = [
Beta = true,
Category = "Other",
ButtonText = { Extension.LoadString("ButtonTitle"), Extension.LoadString("ButtonHelp") },
LearnMoreUrl = "https://powerbi.microsoft.com/",
SourceImage = p2.Icons,
SourceTypeImage = p2.Icons
];

p2.Icons = [
Icon16 = { Extension.Contents("p216.png"), Extension.Contents("p220.png"), Extension.Contents("p224.png"), Extension.Contents("p232.png") },
Icon32 = { Extension.Contents("p232.png"), Extension.Contents("p240.png"), Extension.Contents("p248.png"), Extension.Contents("p264.png") }
];

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

@Anonymous 

Not familiar with Oth2 with api, have a look at this post see if it helps.

https://stackoverflow.com/questions/62580027/how-to-enable-scheduled-refresh-using-a-custom-oauth2-connector-for-google-sprea

 

Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
Anonymous
Not applicable

@Anonymous 

Not familiar with Oth2 with api, have a look at this post see if it helps.

https://stackoverflow.com/questions/62580027/how-to-enable-scheduled-refresh-using-a-custom-oauth2-connector-for-google-sprea

 

Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.