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
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
V-pazhen-msft
Community Support
Community Support

@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
V-pazhen-msft
Community Support
Community Support

@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
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