Forum Discussion
New API connection
- 9 months ago
okay maybe you will need to pass the token as a function then , try to created the following function and query
open blank query and copy past , name the function as GetTokenNew
() as text => let // ----------------------------- // ECI OAuth Token Request // ----------------------------- client_id = "YOUR_CLIENT_ID", client_secret = "YOUR_CLIENT_SECRET", token_url = "https://api-user.integrations.ecimanufacturing.com/oauth2/api-user/token", tokenBody = "grant_type=client_credentials" & "&scope=openid" & "&client_id=" & client_id & "&client_secret=" & client_secret, tokenResponse = Json.Document( Web.Contents( token_url, [ Headers = [ #"Content-Type" = "application/x-www-form-urlencoded", Accept = "application/json" ], Content = Text.ToBinary(tokenBody) ] ) ), // Extract raw token access_token = tokenResponse[access_token], // Return as "Bearer xxxx" BearerToken = "Bearer " & access_token in BearerTokencreate another query and past
let // Call token function BearerToken = GetTokenNew(), api_url = "https://api-jb2.integrations.ecimanufacturing.com/api/v1/job-materials", apiResponse = Json.Document( Web.Contents( api_url, [ Headers = [ Authorization = BearerToken, Accept = "application/json" ] ] ) ), // Convert list/record into table AsList = if apiResponse is list then apiResponse else if apiResponse is record and Record.HasFields(apiResponse, "data") then apiResponse[data] else {}, TableOut = if List.Count(AsList) > 0 then let tbl = Table.FromList(AsList, Splitter.SplitByNothing(), null, null, ExtraValues.Error), firstRecord = tbl{0}[Column1], expanded = Table.ExpandRecordColumn(tbl, "Column1", Record.FieldNames(firstRecord)) in expanded else #table({}, {}) in TableOutbut it's better if you change the get-schedule url(in the first working query) and get the job-materials value in one query first , then try above method
- 9 months ago
Hi Anonymous
Can you try this code and see if it returns a value to pages step ?
let // --------------------------------------------------- // 1. OAuth Token Request (leave your real values) // --------------------------------------------------- client_id = "YOUR_CLIENT_ID", client_secret = "YOUR_CLIENT_SECRET", token_url = "https://api-user.integrations.ecimanufacturing.com/oauth2/api-user/token", tokenResponse = Json.Document( Web.Contents( token_url, [ Headers = [ #"Content-Type" = "application/x-www-form-urlencoded", Accept = "application/json" ], Content = Text.ToBinary( "grant_type=client_credentials" & "&scope=openid" & "&client_id=" & client_id & "&client_secret=" & client_secret ) ] ) ), access_token = tokenResponse[access_token], // --------------------------------------------------- // 2. Paging Function // --------------------------------------------------- PageSize = 200, GetPage = (Skip as number) => let url = "https://api-jb2.integrations.ecimanufacturing.com/api/v1/job-materials" & "?take=" & Number.ToText(PageSize) & "&skip=" & Number.ToText(Skip), response = Json.Document( Web.Contents( url, [ Headers = [ Authorization = "Bearer " & access_token, Accept = "application/json" ] ] ) ), // FIXED: correct field is "Data" (case-sensitive) items = if response is list then response else if response is record and Record.HasFields(response, "Data") then response[Data] else {} in items, // --------------------------------------------------- // 3. Generate pages until empty page encountered // --------------------------------------------------- Pages = List.Generate( () => [Skip = 0, Data = GetPage(0)], each List.Count([Data]) > 0, each [Skip = [Skip] + PageSize, Data = GetPage([Skip] )], each [Data] ), AllItems = List.Combine(Pages), // --------------------------------------------------- // 4. Convert to Table and Expand // --------------------------------------------------- Output = if List.Count(AllItems) = 0 then #table({}, {}) else let tbl = Table.FromList(AllItems, Splitter.SplitByNothing(), null, null, ExtraValues.Error), firstRecord = tbl{0}[Column1], fieldNames = Record.FieldNames(firstRecord), expanded = Table.ExpandRecordColumn(tbl, "Column1", fieldNames, fieldNames) in expanded in Output
Hi kushanNa,
Token Query works like a clock. As you can see the default timeout is 3600min more than enough to pull all the data I need.
JobMaterial Query is not working instead. 2 issues has been highlighted:
1. Token value cannot be converted.
2. Formula.Firewall issue on apiresponse say 'JobMaterials' query reference to other queries.
Thanks again for your help!!
Avaiable to Team call in case it can help to tackle the issue faster.
Giovanni
okay maybe you will need to pass the token as a function then , try to created the following function and query
open blank query and copy past , name the function as GetTokenNew
() as text =>
let
// -----------------------------
// ECI OAuth Token Request
// -----------------------------
client_id = "YOUR_CLIENT_ID",
client_secret = "YOUR_CLIENT_SECRET",
token_url = "https://api-user.integrations.ecimanufacturing.com/oauth2/api-user/token",
tokenBody =
"grant_type=client_credentials" &
"&scope=openid" &
"&client_id=" & client_id &
"&client_secret=" & client_secret,
tokenResponse =
Json.Document(
Web.Contents(
token_url,
[
Headers = [
#"Content-Type" = "application/x-www-form-urlencoded",
Accept = "application/json"
],
Content = Text.ToBinary(tokenBody)
]
)
),
// Extract raw token
access_token = tokenResponse[access_token],
// Return as "Bearer xxxx"
BearerToken = "Bearer " & access_token
in
BearerToken
create another query and past
let
// Call token function
BearerToken = GetTokenNew(),
api_url = "https://api-jb2.integrations.ecimanufacturing.com/api/v1/job-materials",
apiResponse =
Json.Document(
Web.Contents(
api_url,
[
Headers = [
Authorization = BearerToken,
Accept = "application/json"
]
]
)
),
// Convert list/record into table
AsList =
if apiResponse is list then
apiResponse
else if apiResponse is record and Record.HasFields(apiResponse, "data") then
apiResponse[data]
else
{},
TableOut =
if List.Count(AsList) > 0 then
let
tbl = Table.FromList(AsList, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
firstRecord = tbl{0}[Column1],
expanded = Table.ExpandRecordColumn(tbl, "Column1", Record.FieldNames(firstRecord))
in
expanded
else
#table({}, {})
in
TableOut
but it's better if you change the get-schedule url(in the first working query) and get the job-materials value in one query first , then try above method
- Anonymous9 months agoNot applicable
Hi kushanNa ,
Almost there I think.
Everything works until the apiResponse with the error below. Any advice?
Best regards
Giovanni
- kushanNa9 months ago
Super User
Hi Anonymous
can you change the url to
https://api-jb2.integrations.ecimanufacturing.com/api/v1/eci-aps/get-scheduleand see if it give you bank data without any error? if this work then authorization has no issues , this should be an issue with
"https://api-jb2.integrations.ecimanufacturing.com/api/v1/job-materials"maybe you should try and check it with postman or any other api reading tools
or you can try this https://success.qualys.com/discussions/s/article/000007769
- Anonymous9 months agoNot applicable
with get-schedule it works but I don't need that table.
I contacted the vendor but they are not able to help with API.
It's frustating as I don't know what I can do as it's not working.
Are you avaiable for a Team meeting sometimes? Maybe if we looked together we can test something else.
Thanks anyway
- Anonymous9 months agoNot applicable
Hi kushanNa
I was able to make your script works. The problem was about Data Source credential which need to switch to Anonymous.
Now I pull all the tables I face another issue which I dont' expected. It seems only 1000 rows are pulled. Do you may know how to increase this limit as I would like to pull the entire table.
Thanks again for your support.
Giovanni
- kushanNa9 months ago
Super User
Nice!
To retrieve additional pages, you need to include paging parameters. This is mentioned in the document you shared.
Please try the code below and check if it works.
let // Token BearerToken = GetTokenNew(), BaseUrl = "https://api-jb2.integrations.ecimanufacturing.com/api/v1/job-materials", PageSize = 200, // API default, but you can adjust GetPage = (Skip as number) => let url = BaseUrl & "?" & "take=" & Number.ToText(PageSize) & "&skip=" & Number.ToText(Skip), response = Json.Document( Web.Contents( url, [ Headers = [ Authorization = BearerToken, Accept = "application/json" ] ] ) ), data = if response is list then response else if response is record and Record.HasFields(response, "data") then response[data] else {} in data, // Generate list of pages PageList = List.Generate( () => [Skip = 0, Data = GetPage(0)], each List.Count([Data]) > 0, each [Skip = [Skip] + PageSize, Data = GetPage([Skip] + PageSize)], each [Data] ), // Combine all pages into one list CombinedList = List.Combine(PageList), // Convert list to table TableOut = if List.Count(CombinedList) = 0 then #table({}, {}) else let tbl = Table.FromList(CombinedList, Splitter.SplitByNothing(), null, null, ExtraValues.Error), firstRecord = tbl{0}[Column1], expanded = Table.ExpandRecordColumn(tbl, "Column1", Record.FieldNames(firstRecord)) in expanded in TableOut- Anonymous9 months agoNot applicable
Hi kushanNa
I tried but it asked me the page but I expected to automatically load all the pages. Is it possible?
Giovanni