如何对Invoke-RestMethod进行身份验证以列出工件存储库

问题描述 投票:1回答:2

[尝试从Win10桌面使用PowerShell 5.1 Invoke-RestMethod从Artifactory Enterprise v6实例中获取存储库列表,但无法对其进行身份验证。

似乎很简单,但是这个

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

仅返回允许匿名访问的项目。

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

任何提示都缺少$myCred

powershell artifactory credentials
2个回答
0
投票

我过去曾尝试过人工制品,-Credential确实对我没用。

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

使用API​​密钥连接到Artifactory

[Read here,了解如何在人工产品上获取帐户的API密钥。

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

使用基本身份验证和-凭据

如果您确实想使用Get-Credential提示,请确保使用Artifactory中可用的用户名。它与域\用户不同。 from here

$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


0
投票

感谢贾瓦德。这使我可以使用API​​(我的第一次尝试没有正确地形成)。跟随您的链接,发现了其他几个问题(27951561和60325084),这些问题也帮助我获得了凭据。我已经使用凭据以避免混淆源代码中的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   
© www.soinside.com 2019 - 2024. All rights reserved.