MethodsAndPractices / vsteam

PowerShell module for accessing Azure DevOps Services and Azure DevOps Server (formerly VSTS or TFS)
https://methodsandpractices.github.io/vsteam-docs/
MIT License
447 stars 155 forks source link

Invoke-RestMethod: The remote server returned an error: (401) Unauthorized - Creating new branch #503

Open patritasan opened 1 year ago

patritasan commented 1 year ago

Steps to reproduce


$json = New-Object -Type psobject
$json | Add-Member -MemberType NoteProperty -Name "name" -Value "refs/heads/release/release01" -Force
$json | Add-Member -MemberType NoteProperty -Name "oldObjectId " -Value "0000000000000000000000000000000000000000" -Force
$json | Add-Member -MemberType NoteProperty -Name "newObjectId " -Value "[ObjectID_MainBranch]" -Force

$jsonBody = ""
$jsonBody += $json | ConvertTo-Json

Invoke-VSTeamRequest -method Post -body $jsonBody -ProjectName "[ProjectName]" -version "5.0" -Area "repositories" -Resource "[Repository_Name]" -Id "refs" -ContentType 'application/json'

Expected behavior

It should create a new branch "release/release01" as a copy of the "main" branch under the repository "[Repository_Name]". Should be the same as executing the API: https://dev.azure.com/[ORG]/[PROJECT_NAME]/_apis/git/repositories/[REPOSITORY_NAME]/refs?api-version=5.0

The PAT used for the connection has "Code (read, write & manage)" permissions. Executing the POST to the URL using same PAT in postman works.

Actual behavior?

WARNING: An error occurred: The remote server returned an error: (401) Unauthorized. Invoke-RestMethod : The remote server returned an error: (401) Unauthorized. At C:\Users\ptamarit\Documents\WindowsPowerShell\Modules\VSTeam\7.9.0\vsteam.functions.ps1:266 char:18

On Which OS have you tried it?

Windows

What was your server version?

No response

Other server version

No response

Log output of used API

Billing                     : 5.1-preview.1
Build                       : 5.1
Core                        : 5.1
DistributedTask             : 6.0-preview
DistributedTaskReleased     : 5.1
ExtensionsManagement        : 6.0-preview
Git                         : 5.1
Graph                       : 6.0-preview
HierarchyQuery              : 5.1-preview
MemberEntitlementManagement : 6.0-preview
Packaging                   : 6.0-preview
Pipelines                   : 5.1-preview
Policy                      : 5.1
Processes                   : 6.0-preview
Release                     : 5.1
ServiceEndpoints            : 5.0-preview
TaskGroups                  : 6.0-preview
Tfvc                        : 5.1
VariableGroups              : 5.1-preview.1
Version                     : VSTS
Wiki                        : 6.0
WorkItemTracking            : 6.0-preview.1

Log output of $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1645
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1645
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1