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 は、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 に答える