Skip to content

Instantly share code, notes, and snippets.

@mnadjit
Last active June 16, 2021 06:31
Show Gist options
  • Save mnadjit/00700b9b83d818c81d73d7c7d2e6cf4c to your computer and use it in GitHub Desktop.
Save mnadjit/00700b9b83d818c81d73d7c7d2e6cf4c to your computer and use it in GitHub Desktop.
Two methods to perform basic authorization to access a REST API which allows this
// Method 1
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ($username, $securePassword)
Invoke-RestMethod $Uri -Credential $cred
Invoke-WebRequest -Uri $Uri -Method GET -Credential $cred
// Method 2
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
Invoke-RestMethod $Uri -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Invoke-WebRequest -Uri $Uri -Method GET -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment