首页 > 解决方案 > 使用 https://classroom.googleapis.com/v1/courses?pageToken=SomePageToken 的 HTTP 400 错误请求

问题描述

这是我收到错误时正在使用的 powershell 代码片段:

调用代码:

$global:NextPage = $null

GetCourses  # Calls a function to get the first 500 rows

Do {
    GetNextCourses $global:NextPage  # Get next 500 rows using Page Token
} Until ($global:NextPage -eq $null)

The function where I always get an error.  Always at a different rowset / Page Token:

function GetNextCourses ($next) {

$retrycount = 0
$completed = $false
[int]$retries = 5 
[int]$secondsDelay = 10

while (-not $completed) {
    Try{   
        $list_courses_url = "https://classroom.googleapis.com/v1/courses?pageToken=" + $next
        $auth = "Bearer " + $tokens.access_token
        $headers = @{ "Authorization" = $auth }   
        $global:response = Invoke-WebRequest -Uri $list_courses_url -Method GET -headers $headers -ErrorAction Stop -UseBasicParsing
        if ($global:response.StatusCode -eq 200) {
            $completed = $true
        }
    }
    Catch {
        Write-Host $global:Failure.StatusDescription
        if ($retrycount -ge $retries) {
            $global:Failure = $_.Exception.Response
            Write-Host $global:Failure.StatusDescription
            throw
        } else {
            $secondsDelay = $secondsDelay * 2
            Start-Sleep $secondsDelay
            $retrycount++
            $global:Failure = $_.Exception.Response
            if ($global:Failure.StatusCode.Value__ -eq 400) {
                Write-Host $global:Failure.StatusDescription
                GetTokens   
            }
            if ($global:Failure.StatusCode.Value__ -eq 401) {
                GetTokens   
            }
            if ($global:Failure.StatusCode.Value__ -eq 403) {
                GetTokens   
            }
            if ($global:Failure.StatusCode.Value__ -eq 404) {
                Write-Host $global:Failure.StatusDescription
                return
            }
            if ($global:Failure.StatusCode.Value__ -eq 429) {
                Write-Host $global:Failure.StatusDescription
                Write-Host "....... Waiting ........"
                Start-Sleep -Seconds 600 # Additional wait for Resource Exhausted
            }
            if ($global:Failure.StatusCode.Value__ -eq 500) {
                Write-Host $global:Failure.StatusDescription
                Write-Host "....... Waiting ........"
            }
            if ($global:Failure.StatusCode.Value__ -eq 503) {
                Write-Host $global:Failure.StatusDescription
                Write-Host "....... Waiting ........"
            }
        }
    }
}

$rows = $global:response | ConvertFrom-Json 

$global:NextPage = $rows.nextPageToken

....... # 更多代码 ....... # 更多代码

我已经多次运行此代码,总是在不同的页面标记处收到 400 个错误请求错误,所以我知道该代码适用于 X 行。我们在格林维尔县学校有数千门课程,代码总是运行几个小时才能收到 400 错误请求。在我收到 400 错误代码后,获取新令牌或等待 x 秒数不起作用。我尝试在尝试使用新页面令牌之前等待 x 秒,但这不起作用。我认为所有 Web 请求都应该是有效请求,因为我使用相同的代码,只是使用了前一个请求提供的不同页面令牌。

把我的头发拔了好几个星期!任何帮助,将不胜感激。谢谢!

标签: google-apis-explorergoogle-classroom

解决方案


根据此文档FAILED_PRECONDITION (HTTP 400),当由于某些当前状态而不允许请求的操作时,Classroom API 会返回错误。

  • CourseMemberLimitReached表示请求的操作将超过允许的最大课程成员数。
  • CourseNotModifiable表示相关课程处于不允许修改其属性的状态。
  • CourseTeacherLimitReached表示请求的操作将超过课程教师的最大允许数量。
  • UserGroupsMembershipLimitReached表示请求的用户已经是允许的最大组数的成员。
  • AttachmentNotVisible表示指定的一个或多个附件对用户不可见、不属于请求的类型或不存在。

推荐阅读