Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic authentication with the GitHub Api using PowerShell

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?

like image 630
Shaun Luttin Avatar asked Dec 06 '22 23:12

Shaun Luttin


1 Answers

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"}
like image 83
Mathias R. Jessen Avatar answered Dec 08 '22 12:12

Mathias R. Jessen