Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PowerShell使用GitHub Api进行基本身份验证_Powershell_Github_Basic Authentication_Github Api - Fatal编程技术网

使用PowerShell使用GitHub Api进行基本身份验证

使用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

我一直在尝试使用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",
     "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"}