title: HTTP 400 Bad Request using https://classroom.googleapis.com/v1/courses?pageToken=SomePageToken
body: <p>Here is a powershell code snippet I am using when I get the error:</p>
  
  <p>Calling code:</p>
  
  <pre><code>$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
  </code></pre>
  
  <p>.......   # More code
  .......   # More code</p>
  
  <p>I have ran this code many times getting 400 bad request errors always at different Page Tokens so I know the code works right for X rows.  We have thousands of courses at Greenville County Schools and the code always runs for several hours before getting the 400 bad request.  After I get the 400 error code getting a new token or waiting x amount of seconds does not work. I have tried waiting x seconds before trying to use a new page token and this does not work.  I would think that all of the web requests should be valid requests since I am using the same code only a different Page Token that is supplied by the previous request.</p>
  
  <p>Pulling my hair out for weeks!  Any help would be appreciated.  Thanks!</p>