Basic authentication with the GitHub Api using PowerShell

こ雲淡風輕ζ 提交于 2019-12-01 08:22:42

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"}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!