Skip to content

Instantly share code, notes, and snippets.

@Wind010
Created February 25, 2023 05:13
Show Gist options
  • Select an option

  • Save Wind010/59d245bd3b3b58bf8c11ec61e0739f0c to your computer and use it in GitHub Desktop.

Select an option

Save Wind010/59d245bd3b3b58bf8c11ec61e0739f0c to your computer and use it in GitHub Desktop.
Get API key via basic auth. Curl.exe -u functionality
function GetApiKey()
{
Param(
[Parameter(Mandatory=$false, Position=0, ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()]
[string] $userName,
[Parameter(Mandatory=$false, Position=1, ValueFromPipeline=$false)]
[SecureString] $useStaging
)
$plainPassword = (ConvertFrom-SecureString $password -AsPlainText)
$pair = "$($userName):$($plainPassword)"
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCredentials"
$headers = @{
Authorization = $basicAuthValue
}
[string] $uri = "https://api.something.org/auth"
$authResponse = Invoke-WebRequest -Uri $uri -Method GET -Headers $headers
return $authResponse.Headers['X-Api-Key']
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment