Skip to content

Invoke-RestMethod : The remote server returned an error: (401) Unauthorized #3

Description

@kwab

OS: Windows Server 2012
PowerShell: v5
DDNS service: Google
Cause: Powershell does not pass the password correctly.
Solution: Encode the user credentials in Base64 and add to request headers.

$authInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$username`:$password"))
$headers = @{"X-Requested-With"="$userAgent";"Authorization"="Basic $authInfo"}
$result = Invoke-Restmethod -Method GET -Uri $uri -headers $headers

Credit: https://success.qualys.com/support/s/article/000003066#:~:text=401%20Unauthorized%20means%20that%20the,added%20to%20the%20request%20headers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions