2015-01-15 85 views
4

我一直在尝试使用PowerShell执行basic authentication with the GitHub Api。以下内容不起作用:使用PowerShell使用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进行基本身份验证?

回答

10

基本认证的基本预期,您发送的凭证在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"} 
相关问题