I have a PowerShell script that has worked for a long time and retrieves the last 100 comments from a user through an API URL such as `https://api.reddit.com/user/spez.json?limit=100&after=`. I added `-UseBasicParsing` a few months ago, but the request only recently started failing with `WebCmdletWebResponseException` and `InvalidOperation` errors. The current code is `$response = Invoke-WebRequest -UseBasicParsing $url | ConvertFrom-Json`. Is unauthenticated access no longer supported, and what changes are needed to make the request work again?
2 Answers
The browser result can be misleading. If the response starts with CSS such as `.theme-light` or `:root`, you received an HTML web page rather than JSON from the API. Browser requests may include cookies and other authentication details that your PowerShell command does not have, so copying the visible URL alone is not enough.
Unauthenticated API traffic appears to be blocked now because of a policy change. You need to create an OAuth access token, send it in an `Authorization: Bearer ` header, and make authenticated API requests through the OAuth API host instead of the public API host. The request should also include a descriptive user agent.

That explains the timing. I added the access token to the request headers and switched the request URL to the OAuth host, and the script started working again.