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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
shivendr
Helper I
Helper I

Need Help in M-code to get all the pages in the API, Getting Token Error

Hi Community,

I am taking data from Hubspot Private app API, I am using below M-code to get all the data, But its giving some syntax error(Token Error).

API is calling next page in this format : 
" "paging": { "next": { "after": "5852", "link": "https://api.hubapi.com/crm/v3/objects/CONTACT/?limit=100&after=5852""

I written M-code to get data from this API in above format :
"

let
// Define the base URL
baseUrl = "https://api.hubapi.com/crm/v3/objects/CONTACT/?limit=100&after=",
afterValue = "5852", // Replace this with your initial "after" value
maxPages = 10, // Set the maximum number of pages to retrieve

// Initialize variables to store the results
allPages = {},
pageNumber = 1,

// Function to get a page of data
getPage = (after) =>
let
// Construct the URL for the page
pageUrl = baseUrl & after,

// Make the HTTP request to the page URL
pageContent = Json.Document(Web.Contents(pageUrl)),

// Extract the "paging" information from the page
paging = Record.FieldOrDefault(pageContent, "paging", null),

// Extract the "next" link from the paging information
nextLink = Record.FieldOrDefault(paging, "next", null)
in
if nextLink <> null then
[Data = pageContent, NextLink = Text.BetweenDelimiters(nextLink, "after=", "&")]
else
[Data = pageContent, NextLink = null],

// Function to retrieve all pages of data
retrieveAllPages = () =>
let
nextLink = afterValue,
currentData = null
in
while nextLink <> null and pageNumber <= maxPages do
let
pageResult = getPage(nextLink),
pageData = pageResult[Data],
nextLink = pageResult[NextLink]
in
if currentData = null then
currentData = pageData
else
currentData = Table.Combine({currentData, pageData}),
pageNumber = pageNumber + 1
in
currentData
in
retrieveAllPages()

"

I am getting Error in above bold code, Giving Token error.


Help is appriciated, Thanks in Advance!

1 REPLY 1
lbendlin
Super User
Super User

Read about Headers, RelativePath and Query options for Web.Contents - PowerQuery M | Microsoft Learn

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors