如何验证 Invoke-RestMethod 以列出人工存储库

how to authenticate Invoke-RestMethod to list artifactory repositories

尝试从 Artifactory Enterprise v6 实例获取存储库列表,使用 PowerShell 5.1 Invoke-RestMethod 从 Win10 桌面,但看不到让它进行身份验证。

看起来很简单,但这

$myCred = Get-Credential notStanley
$lstART = Invoke-RestMethod -URI https://<myserver>/artifactory/api/repositories -Credential $myCred

只有 returns 允许匿名访问的项目。

如果我打开浏览器并登录到该 Artifactory 实例,然后我可以粘贴上面的 URI 并获取我的帐户有权访问的所有存储库的完整列表。

有什么提示 $myCred 遗漏了什么吗?

我过去曾尝试使用 artifactory,但 -Credential 对我来说并不奏效。

我尝试了 API 方法,它更简单易用。

使用 API 密钥连接到 Artifactory

Read here 了解如何获取您在 artifactory 上的帐户的 API 密钥。

$header = @{"X-JFrog-Art-Api" = "yourAPIKey"}
Invoke-RestMethod -URI https://<myserver>/artifactory/api/repositories -Headers $header

使用基本身份验证和-Credential

如果您确实想要使用 Get-Credential 提示,请确保您使用的用户名适用于 Artifactory。它与 domain\user 不同。

$login = Get-Credential -Message "Enter Credentials for Artifactory"

#invalid creds.. but its ok. Need to tell invoke-restmethod to use Basic Auth.
$headers = @{ Authorization = "Basic Zm9vOmJhcg==" }  

# use -Credential to override the credentials.
$new = Invoke-RestMethod -URI https://<server>/artifactory/api/repositories -Headers $headers -Credential $login

谢谢贾瓦德。这让我开始使用 API (我的第一次尝试没有完全正确地形成)。按照您的链接找到了其他几个问题(27951561 和 60325084),这些问题也帮助我获得了 Credential。我已经使用 Credential 来避免混淆源代码中的 API 键。

我的基础骨架现在看起来像:

# get standard PowerShell credential
$myCred  = Get-Credential -Message "just <name> for this server, not '<domain>\<name>'"

# format credential for Artifactory API
$credUser    = $myCred.UserName                                   # extract user name
$credPswd    = $myCred.GetNetworkCredential().password            # extract user password
$credPair    = "${credUser}:${credPswd}"                          # concatenate into BasicAuth format
$credBytes   = [System.Text.Encoding]::ASCII.GetBytes($credPair)  # convert byte values to text
$cred64      = [System.Convert]::ToBase64String($credBytes)       # condense a bit more secure string RFC2045-MIME    
$credAuth    = "Basic $cred64"                                    # intermediate formatting 
$restHeaders = @{ Authorization = $credAuth }                     # initialize web headers

# clear collection array
$cfgSite = @()

# locate server
$lstURL "https://<myserver>/artifactory/api/repositories"

# get list of repositories
$theseRepo = Invoke-RestMethod -Headers $restHeaders -Uri $lstURL 

# collect each configuration
ForEach ($thisRepo in $theseRepo)
   {
   $thisURI  = $lstURL + $thisRepo.key
   $thisCfg  = Invoke-RestMethod -Headers $restHeaders -Uri $thisURI    
   $thisCfg  | Add-Member -NotePropertyName "SITE" -NotePropertyValue "$thisSite"     
   $cfgSite += $thisCfg
   }

# output to file
$cfgAll | Export-Csv .\lstArtRepoConf.csv -NoTypeInformation