M query:
let
// Function to get a new access token using the refresh token
GetNewAccessToken = () =>
let
tokenBody = Text.ToBinary(
"grant_type=refresh_token" &
"&client_id=" & client_id &
"&client_secret=" & Uri.EscapeDataString(client_secrets) &
"&refresh_token=" & Uri.EscapeDataString(RefreshToken) &
),
tokenResponse = Web.Contents(tokenUrl, [
Headers = [#"Content-Type"="application/x-www-form-urlencoded"],
Content = tokenBody
]),
tokenJson = Json.Document(tokenResponse),
new_access_token = tokenJson[access_token]
in
new_access_token,
// Check if the access token is valid (this is an assumption; replace with your logic if possible)
// For demonstration purposes, let's assume we always refresh the token for simplicity.
access_token = GetNewAccessToken(),
// Create a query string with the new access token
authQuery = "?access_token=" & access_token,
// Append the query string to the API URL
urlWithAuth = apiURL & authQuery,
// Make the API request
response = Web.Contents(urlWithAuth, [
Headers = [
#"Content-Type" = "application/json"
]
]),
// Parse the JSON response to extract the data
data = Json.Document(response)
in
data
ERROR
Details:
DataSourceKind=Web