Created
February 25, 2023 05:13
-
-
Save Wind010/59d245bd3b3b58bf8c11ec61e0739f0c to your computer and use it in GitHub Desktop.
Get API key via basic auth. Curl.exe -u functionality
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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