【问题标题】:Basic authentication with the GitHub Api using PowerShell使用 PowerShell 通过 GitHub Api 进行基本身份验证
【发布时间】:2015-03-14 04:42:34
【问题描述】:
我一直在尝试使用 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"
}
我们如何通过 GitHub Api 使用 PowerShell 进行基本身份验证?
【问题讨论】:
标签:
powershell
github
basic-authentication
github-api
【解决方案1】:
Basic auth 基本上希望您以以下形式在 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"}