我一直在尝试使用PowerShell basic authentication with the GitHub Api。以下不起作用:
> $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"
}
我们如何使用PowerShell与GitHub Api进行基本身份验证?
答案 0 :(得分:12)
基本身份验证基本上希望您使用以下格式在Authorization
标头中发送凭据:
'Basic [base64("username:password")]'
在PowerShell中可以转换为:
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)
}
现在你可以这样做:
$BasicCreds = Get-BasicAuthCreds -Username "Shaun" -Password "s3cr3t"
Invoke-WebRequest -Uri $GitHubUri -Headers @{"Authorization"="Basic $BasicCreds"}