22Param (
33 [Parameter (Mandatory = $False )]
44 [array ] $ArtifactList ,
5- [Parameter (Mandatory = $False )]
5+ [Parameter (Mandatory = $True )]
66 [string ] $ArtifactPath ,
7+ [Parameter (Mandatory = $False )]
8+ [string ] $APIKey ,
79 [string ] $SourceBranch ,
810 [string ] $DefaultBranch ,
911 [string ] $RepoName ,
@@ -27,17 +29,13 @@ function Get-ApiViewBearerToken()
2729{
2830 $audience = " api://apiview"
2931 try {
30- $tokenResponse = az account get-access - token -- resource $audience -- output json | ConvertFrom-Json
32+ $tokenResponse = az account get-access - token -- resource $audience -- output json 2> $null | ConvertFrom-Json
3133 if ($tokenResponse -and $tokenResponse.accessToken ) {
3234 return $tokenResponse.accessToken
3335 }
34- else {
35- Write-Error " Failed to acquire access token - no token in response"
36- return $null
37- }
36+ return $null
3837 }
3938 catch {
40- Write-Error " Failed to acquire access token: $ ( $_.Exception.Message ) "
4139 return $null
4240 }
4341}
@@ -99,15 +97,24 @@ function Upload-SourceArtifact($filePath, $apiLabel, $releaseStatus, $packageVer
9997
10098 $uri = " ${APIViewUri} /upload"
10199
102- # Get Bearer token for authentication
100+ # Try Bearer token first, fall back to API key
103101 $bearerToken = Get-ApiViewBearerToken
104- if (-not $bearerToken ) {
105- return 401
102+ if ($bearerToken ) {
103+ $headers = @ {
104+ " Authorization" = " Bearer $bearerToken " ;
105+ " content-type" = " multipart/form-data"
106+ }
106107 }
107-
108- $headers = @ {
109- " Authorization" = " Bearer $bearerToken " ;
110- " content-type" = " multipart/form-data"
108+ elseif ($APIKey ) {
109+ Write-Warning " ##[warning]Bearer token acquisition failed - falling back to API key."
110+ $headers = @ {
111+ " ApiKey" = $APIKey ;
112+ " content-type" = " multipart/form-data"
113+ }
114+ }
115+ else {
116+ Write-Error " No authentication available. Either configure AzureCLI@2 task or provide APIKey."
117+ return 401
111118 }
112119
113120 try
@@ -149,14 +156,22 @@ function Upload-ReviewTokenFile($packageName, $apiLabel, $releaseStatus, $review
149156
150157 Write-Host " Request to APIView: $uri "
151158
152- # Get Bearer token for authentication
159+ # Try Bearer token first, fall back to API key
153160 $bearerToken = Get-ApiViewBearerToken
154- if (-not $bearerToken ) {
155- return 401
161+ if ($bearerToken ) {
162+ $headers = @ {
163+ " Authorization" = " Bearer $bearerToken "
164+ }
156165 }
157-
158- $headers = @ {
159- " Authorization" = " Bearer $bearerToken "
166+ elseif ($APIKey ) {
167+ Write-Warning " ##[warning]Bearer token acquisition failed - falling back to API key. Please migrate to using AzureCLI@2 task with service connection."
168+ $headers = @ {
169+ " ApiKey" = $APIKey
170+ }
171+ }
172+ else {
173+ Write-Error " No authentication available. Either configure AzureCLI@2 task or provide APIKey."
174+ return 401
160175 }
161176
162177 try
0 commit comments