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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Sri1999
Frequent Visitor

Authentication Issue while connecting to API with OAuth 2.0

Authentication.png

I am able to get the access token Dynamically, but could not able to retrieve data.
Please help me with this issue. Below is the M code for your reference

Thank you.


let // Define API details
tokenUrl = "https://auth.servicetitan.io/connect/token",
apiUrl = "https://api.servicetitan.io/crm/v2/tenant/{tenant_id}/export/customers",
clientId = "%client_id%",
clientSecret = "%client_secret%",


// Get access token
tokenResponse = Json.Document(Web.Contents(tokenUrl, [
Content = Text.ToBinary("grant_type=client_credentials&client_id=" & clientId & "&client_secret=" & clientSecret & "&scope=" & scope),
Headers = [
Authorization = "Basic " & Binary.ToText(Text.ToBinary(clientId & ":" & clientSecret), BinaryEncoding.Base64),
Accept = "application/json",
#"Content-Type" = "application/x-www-form-urlencoded"
],
ManualStatusHandling = {400, 401, 403}
])),

accessToken = if Record.HasFields(tokenResponse, "access_token") then tokenResponse[access_token] else error "Failed to get access token",

// Call API with the access token
apiResponse = Web.Contents(apiUrl, [Headers=[Accept="application/json"]])

// Return data

in apiResponse

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

you need to present the token as part of your last API call. It goes into the Headers part.

 

Please follow the documentation.  https://learn.microsoft.com/en-us/powerquery-m/web-contents

View solution in original post

3 REPLIES 3
Sri1999
Frequent Visitor

Thanks for your help. It actually worked.

v-menakakota
Community Support
Community Support

Hi @Sri1999 ,

Thank you @lbendlin  for providing possible solution. I hope this information is helpful. Please let me know if you have any further questions or if you'd like to discuss this further. If this answers your question, please Accept it as a solution and give it a 'Kudos' so others can find it easily.

Thank you.

lbendlin
Super User
Super User

you need to present the token as part of your last API call. It goes into the Headers part.

 

Please follow the documentation.  https://learn.microsoft.com/en-us/powerquery-m/web-contents

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

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.