1

While I was trying to GET group's 1000 threads using Microsoft Graph API on PowerShell, 504 gateway error is happening and the code stopped.

But if I re-execute the code without any change, it ran well. Why this happens and how should I avoid this issue?

$apiUrl53 = "https://graph.microsoft.com/beta/groups/" + $groups.id + "/threads?top=1000"
    $Data = Invoke-WebRequest -Headers $global:__authHeader -Uri $apiUrl53 -Method Get

Error Message :

 + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Karen
  • 73
  • 7
  • I'm facing a similar issue https://stackoverflow.com/questions/56850282/microsoft-graph-api-errors – eran otzap Jul 02 '19 at 12:38
  • what is the request-id returned with your response? – Jeremy Thake MSFT Jul 03 '19 at 23:35
  • @JeremyThakeMSFT Can you guide me how to print from Powershell? – Karen Jul 17 '19 at 02:39
  • You can output the error message try { $response = Invoke-WebRequest -Uri "www.microsoft.com/unkownhost" -ErrorAction Stop # This will only execute if the Invoke-WebRequest is successful. $StatusCode = $Response.StatusCode } catch { $StatusCode = $_.Exception.Response.StatusCode.value__ } $StatusCode 404 See example 7 here https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6 – Jeremy Thake MSFT Jul 18 '19 at 16:05

1 Answers1

0

Is there any reason that you are trying to get the top 1000?

You can 'page' through the groups in records of 100 for instance, so you aren't returning such a huge payload in one response.

There is a great video on this that shows how to use the skiptoken https://learn.microsoft.com/en-us/graph/paging?context=graph%2Fapi%2F1.0&view=graph-rest-1.0

For example, the following URL requests all the users in an organization with a page size of 5, specified with the $top query parameter:

https://graph.microsoft.com/v1.0/users?$top=100

If the result contains more than five users, Microsoft Graph will return an @odata:nextLink property similar to the following along with the first page of users.

"@odata.nextLink": "https://graph.microsoft.com/v1.0/users?$top=100&$skiptoken=X%274453707 ... 6633B900000000000000000000%27"

You can retrieve the next page of results by sending the URL value of the @odata:nextLink property to Microsoft Graph.

https://graph.microsoft.com/v1.0/users?$top=100&$skiptoken=X%274453707 ... 6633B900000000000000000000%27

Microsoft Graph will continue to return a reference to the next page of data in the @odata:nextLink property with each response until all pages of the result have been read.

Jeremy Thake MSFT
  • 2,058
  • 2
  • 13
  • 11