使用PowerShell使用GitHub Api进行基本身份验证
我一直在尝试使用PowerShell。以下各项不起作用:使用PowerShell使用GitHub Api进行基本身份验证,powershell,github,basic-authentication,github-api,Powershell,Github,Basic Authentication,Github Api,我一直在尝试使用PowerShell。以下各项不起作用: > $cred = get-credential # type username and password at prompt > invoke-webrequest -uri https://api.github.com/user -credential $cred Invoke-WebRequest : { "message":"Requires authentication", "docu
> $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进行基本身份验证?基本上,基本身份验证希望您以以下形式在
授权
头中发送凭据:
'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"}