Basic authentication with the GitHub Api using PowerShell

穿精又带淫゛_ 提交于 2019-12-01 05:32:56

问题


I have been trying to do basic authentication with the GitHub Api with PowerShell. The following do not work:

 > $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"
 }

How do we do basic authentication using PowerShell with the GitHub Api?


回答1:


Basic auth basically expects that you send the credentials in an Authorization header in the following form:

'Basic [base64("username:password")]'

In PowerShell that would translate to something like:

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)
}

And now you can do:

$BasicCreds = Get-BasicAuthCreds -Username "Shaun" -Password "s3cr3t"

Invoke-WebRequest -Uri $GitHubUri -Headers @{"Authorization"="Basic $BasicCreds"}


来源:https://stackoverflow.com/questions/27969362/basic-authentication-with-the-github-api-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!