0

我正在尝试为其 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"

我搜索了一段时间,找不到解决方案。有没有人遇到过这个问题?提前致谢。

4

2 回答 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"
于 2016-11-03T22:26:18.243 回答
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
    }
}
于 2016-11-15T13:46:04.550 回答