分页时如何从 Okta 的 API List Users 中的响应标头获取 Next Link?

问题描述 投票:0回答:2

我正在尝试为其 API 端点 List Users 实现 Okta 的分页。看起来为了分页,必须通过响应中的传入标头获取下一个链接。当通过命令行的 cUrl 或 Postman 执行 List Users API 端点时,标头中的所有内容看起来都很棒,但问题是当使用 cUrl 或 guzzle 从 PHP 脚本运行它时,Link 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"

我搜索了一段时间,没有找到解决办法。以前有人遇到过这个问题吗?预先感谢。

php curl okta pagination okta-api
2个回答
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"

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 Invoke-PagedMethod function from https://github.com/gabrielsroka/OktaAPI.psm1/blob/master/Modules/OktaAPI.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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.