我正在调用Google Calendar API,并请求我日历中所有事件的列表。但是,我只返回一个事件。日历中有40多个事件。
$CalURL="https://www.googleapis.com/calendar/v3/calendars/primary/events"
$header=@{
Authorization = "Bearer $($tokens.access_token)"
maxResults='2500'
}
Invoke-RestMethod -Headers $header -Uri $CalURL -Method Get -ContentType 'application/json'
答案 0 :(得分:0)
使用Google Calendar API获取事件时,重要的是要知道所有结果都是分页的,即使您的所有事件都适合一个页面,Google也不要求将所有事件都返回到一页中。
这是Calendar API reference中maxResults
参数的定义:
在一个结果页面上返回的最大事件数。 即使有更多与查询匹配的事件,结果页面中的事件数也可能小于此值,或根本没有。响应中的非空白
nextPageToken
字段可以检测到不完整的页面。默认情况下,该值为250个事件。页面大小不能超过2500个事件。可选。
要解决此问题,必须实现一个循环以获取所有事件并使用nextPageToken
:
$CalURL="https://www.googleapis.com/calendar/v3/calendars/primary/events"
$header=@{
Authorization = "Bearer $($tokens.access_token)"
maxResults='2500'
}
$result = Invoke-RestMethod -Headers $header -Uri $CalURL -Method Get -ContentType 'application/json'
$FullResults = @()
$result.items | ForEach-Object { $FullResults += $_ }
# Check if the results have a value for nextPageToken
while($null -ne $result.nextPageToken) {
$header=@{
Authorization = "Bearer $($tokens.access_token)"
maxResults='2500'
nextPageToken="$($result.nextPageToken)"
}
$result = Invoke-RestMethod -Headers $header -Uri $CalURL -Method Get -ContentType 'application/json'
$result.items | ForEach-Object { $FullResults += $_ }
}
$FullResults
以上内容应循环播放,直到收到所有页面为止