如何修改这个M查询,使PowerBI从restapi同时获取所有页面?
这是我当前的代码。当然,它只返回第一页:如何修改这个M查询,使PowerBI从restapi同时获取所有页面?,rest,pagination,powerbi,response-headers,Rest,Pagination,Powerbi,Response Headers,这是我当前的代码。当然,它只返回第一页: let url = "https://id.myapi.ac.uk/connect/token", GetJson = Web.Contents(url, [ Headers = [#"Content-Type"="application/x-www-form-urlencoded;"], Content = Text.ToBinary(&quo
let
url = "https://id.myapi.ac.uk/connect/token",
GetJson = Web.Contents(url,
[
Headers = [#"Content-Type"="application/x-www-form-urlencoded;"],
Content = Text.ToBinary("client_id=mechamp&client_secret=notthislol&grant_type=client_credentials&scope=myapi")
]
),
FormatAsJson = Json.Document(GetJson),
// Gets token from the Json response
AccessToken = FormatAsJson[access_token],
AccessTokenHeader = "bearer " & AccessToken,
// Uses the GET search method using the bearer token from the previous POST oauth2/token method
GetJsonQuery = Web.Contents("https://data.api.myapi.ac.uk/activities?recordState=0&pageSize=100",
[
Headers = [#"Authorization"=AccessTokenHeader,
#"api-version"="2",
#"Accept"="application/json"]
]
),
FormatAsJsonQuery = Json.Document(GetJsonQuery),
Name"})
in
FormatAsJsonQuery
我以前在Excel中实现了这一点,使用了一个封装在VBA创建的批处理文件中的curl请求。忽略已在M query中工作的部分,批处理文件如下所示:
rem ommiting getting the token and setting it as %token%
set pagenum=1
set pagesize=100
curl "{https://data.api.myapi.ac.uk/activities?&page=%pagenum%&pageSize=1&recordstate=0}" -G --header "api-version: 2" --header "Authorization: Bearer %token%" --header "Accept: application/json" --trace-ascii %userprofile%\api\totalpages.txt
for /F "delims=" %%a in ('findstr /I "totalCount" %userprofile%\api\totalpages.txt') do set "totalrecords=%%a"
set totalrecords=%totalrecords:,"t=%
set totalrecords=%totalrecords:0000: X-Pagination: {"currentPage":1,"pageSize":1otalCount":=%
rem del %userprofile%\api\totalpages.txt
set /a totalpages = %totalrecords%/%pagesize%
set /a totalpages = %totalpages%+1
start "" /min curl "{https://data.api.heat.ac.uk/activities?&page=%pagenum%&pageSize=%pagesize%&recordstate=0&sort=-activityStartDate}" -G --data-urlencode "fields=%fields%" --header "api-version: 2" --header "Authorization: Bearer %token%" --header "Accept: application/xml" --output %userprofile%\api\file%pagenum%.txt
:loop
set /A pagenum=pagenum+1
start "" /min curl "{https://data.api.heat.ac.uk/activities?&page=%pagenum%&pageSize=%pagesize%&recordstate=0&sort=-activityStartDate}" -G --data-urlencode "fields=%fields%" --header "api-version: 2" --header "Authorization: Bearer %token%" --header "Accept: application/xml" --output %userprofile%\api\file%pagenum%.txt
if %pagenum% LSS %totalpages% (goto :loop)
rem omitting lines that copy all the destination files into one and format into into a valid single json
exit
您可以看到它首先发出请求并将原始输出转储到一个文件中,包括告诉我有多少条记录的标题,然后我可以使用这些标题计算出有多少页。我在谷歌上搜索了如何在PowerBI中获取标题,但在他们的不明智行为中,似乎微软已经决定没有人需要它们,并且会用它们做坏事
我怎样才能知道有多少页,并一次全部请求它们
编辑:下面是试图找出有多少页。另一种方法是继续请求下一个页面,直到服务器返回404,如果它同时工作就可以了。换句话说,如果这可以在看不到响应头的情况下工作,那对我来说就足够了。我是power bi的新手,所以如果你能为我指出一个脚本的方向,那就太好了
这篇博文被广泛引用,以证明这种方法,但不幸的是,它已不再可用:Internet archive也不包含此内容
我做不到的事情:
“@odata.count”未找到是我收到的错误,显然此API不支持该错误
有一个叫做WebMethod.Head的引用,听起来很有趣,但是没有提供关于如何使用它的文档:
我试图重新创建这个声称接收响应头的脚本,但语法不起作用
显然,对于某些API来说,它非常简单
= try GetJsonQuery[next_page_url] otherwise null
对我来说它是空的
Feed告诉我在这个端点没有Odata
GetJsonQuery = OData.Feed("https://data.myapi.ac.uk/activities?recordState=0&pageSize=100",
[
#"Authorization"="bearer " & "AccessToken",
#"api-version"="2",
#"Accept"="application/json"
], [IncludeMetadataAnnotations="*"] ) in
GetJsonQuery
有什么想法吗?请随便谁?!