2015-01-15 175 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"} 
相關問題