cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Abawa
New Member

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

@Abawa 

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-c...

 

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

@Abawa 

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-c...

 

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

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Get Ready for Power BI Dev Camp

Power BI Dev Camp - June 24th

Mark your calendars and join us for our next Power BI Dev Camp!

Top Solution Authors
Top Kudoed Authors