4
votes

I have been trying to do basic authentication with the GitHub Api with PowerShell. The following do not work:

 > $cred = get-credential
 # type username and password at prompt

 > invoke-webrequest -uri https://api.github.com/user -credential $cred

 Invoke-WebRequest : {
     "message":"Requires authentication",
     "documentation_url":"https://developer.github.com/v3"
 }

How do we do basic authentication using PowerShell with the GitHub Api?

1

1 Answers

15
votes

Basic auth basically expects that you send the credentials in an Authorization header in the following form:

'Basic [base64("username:password")]'

In PowerShell that would translate to something like:

function Get-BasicAuthCreds {
    param([string]$Username,[string]$Password)
    $AuthString = "{0}:{1}" -f $Username,$Password
    $AuthBytes  = [System.Text.Encoding]::Ascii.GetBytes($AuthString)
    return [Convert]::ToBase64String($AuthBytes)
}

And now you can do:

$BasicCreds = Get-BasicAuthCreds -Username "Shaun" -Password "s3cr3t"

Invoke-WebRequest -Uri $GitHubUri -Headers @{"Authorization"="Basic $BasicCreds"}