使用PowerShell对GitHub Api进行基本身份验证 [英] Basic authentication with the GitHub Api using PowerShell

查看:407
本文介绍了使用PowerShell对GitHub Api进行基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用PowerShell执行使用GitHub Api的基本身份验证。以下行不通:

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

我们如何使用PowerShell与GitHub Api进行基本身份验证?

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

推荐答案

基本身份验证基本上期望您在 Authorization 标题如下:

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

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

在PowerShell中,它可以翻译为:

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

这篇关于使用PowerShell对GitHub Api进行基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆