我正在尝试为他们的API端点List Users实现Okta的分页。看起来为了分页,必须通过其响应中的传入标头获取下一个链接。通过命令行的cUrl或Postman执行List Users API端点时,标题中的一切看起来都很棒,但问题是当使用cUrl或guzzle从PHP脚本运行它时,链接 html标记是从标题中删除,如下所示:
HTTP/1.1 200 OK
Date: Thu, 03 Nov 2016 19:36:34 GMT
Server: nginx
Content-Type: application/json;charset=UTF-8
Vary: Accept-Encoding
X-Okta-Request-Id: WBuTwqhxlYz3iu5PY1jqHQZZBMU
X-Rate-Limit-Limit: 1200
X-Rate-Limit-Remaining: 1198
X-Rate-Limit-Reset: 1478201841
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: 0
Link: ; rel="self"
Strict-Transport-Security: max-age=315360000
标题应该看起来像:
HTTP/1.1 200 OK
Content-Type: application/json
Link: <https://your-domain.okta.com/api/v1/users?limit=200>; rel="self"
Link: <https://your-domain.okta.com/api/v1/users? after=00ud4tVDDXYVKPXKVLCO&limit=200>; rel="next"
我搜索了一会儿但找不到解决方案。有没有人遇到过这个问题?提前谢谢。
答案 0 :(得分:2)
确保您在请求中包含Accept: application/json
标头。我的猜测是PHP的cURL或Guzzle使用不同的MIME类型,如text/plain
。
我能够使用以下curl
命令重现您的问题,该命令没有结果:
curl --verbose \
--header "Authorization: SSWS ${API_KEY}" \
--header "Content-Type: application/json" \
--header "Accept: text/plain" \
"https://${ORG}/api/v1/users?limit=1" 2>&1 | grep 'Link: '
但是,如果我将Accept:
标题更改为application/json
,我会收到Link:
标题:
curl --verbose \
--header "Authorization: SSWS ${API_KEY}" \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
"https://${ORG}/api/v1/users?limit=1" 2>&1 | grep 'Link: '
< Link: <https://example.okta.com/api/v1/users?limit=1>; rel="self"
< Link: <https://example.okta.com/api/v1/users?after=012a3b456cdefgHijK7l8&limit=1>; rel="next"
答案 1 :(得分:0)
我在搜索其他内容时找到了您的帖子,我最近解决了这个问题,这是我的解决方案。所有这些都是用PowerShell编写的。
#Function to automatically get all listings by pagination, this function will use the default Okta Limit parameter. Which is 1000 as the time of this making.
#Invoke-OktaPagedMethod is based on the _oktaRecGet() function from https://github.com/mbegan/Okta-PSModule/blob/master/Okta.psm1
function Invoke-OktaPagedMethod {
param
(
[string]$Uri,
[array]$col,
[int]$loopcount = 0
)
try {
#[System.Net.HttpWebResponse]$response = $request.GetResponse()
$OktaResponse = Invoke-WebRequest -Method Get -UseBasicParsing -Uri $Uri -Headers $OktaHeaders -TimeoutSec 300
#Build an Hashtable to store the links
$link = @{}
if ($OktaResponse.Headers.Link) { # Some searches (eg List Users with Search) do not support pagination.
foreach ($header in $OktaResponse.Headers.Link.split(",")) {
if ($header -match '<(.*)>; rel="(.*)"') {
$link[$matches[2]] = $matches[1]
}
}
}
$link = @{
next = $link.next
}
try {
$psobj = ConvertFrom-Json -InputObject $OktaResponse.Content
$col = $col + $psobj
} catch {
throw "Json Exception : " + $OktaResponse
}
} catch {
throw $_
}
if ($link.next) {
$loopcount++
if ($oktaVerbose) { Write-Host "fetching next page $loopcount : " -ForegroundColor Cyan}
Invoke-OktaPagedMethod -Uri $link.next -col $col -loopcount $loopcount
} else {
return $col
}
}