1
votes

I'm trying to run the following API 'Patch' command from powershell which I'm looking to execute a release in Azure DevOps ...

$deploy = Invoke-RestMethod -Uri $patchurl -Method Patch -Body $body -Headers $header -ContentType "application/json-patch+json"

The url example is below

https://vsrm.dev.azure.com/test/Fixed%20Income/_apis/Release/releases/567/environments/1072?api-version=6.0-preview.6

The body of the patch call is ...

    "Status": "inProgress",
    "scheduledDeploymentTime": null,
    "comment": null,
    "variables": {}

Running the invoke command from powershell returns the following response ...

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"TF400898: An Internal Error Occurred. Activity Id: 759888ab-9828-4cb6-ba2f-e90cde1cd39a.","typeName":"System.Web.Http.HttpResponseException, System.Web.Http","typeKey":"HttpResponseException","errorCode":0,"eventId":0}

When I run the same url to run a 'patch' request from Postman I get the below response, which is what I'm expecting to get from Powershell.

"$id": "1", "innerException": null, "message": "TF400813: The user '' is not authorized to access this resource.", "typeName": "Microsoft.TeamFoundation.Framework.Server.UnauthorizedRequestException, Microsoft.TeamFoundation.Framework.Server", "typeKey": "UnauthorizedRequestException", "errorCode": 0, "eventId": 3000

Any ideas what I'm doing wrong?

1

1 Answers

2
votes

According to your description, when you call this REST API from Postman, the response returns an error: "message": "TF400813: The user" is not authorized to access this resource." The cause of this problem is incorrect authorization.

Please create a new personal access token in the organization first, then select the type: Basic Auth in the Authorization tab, and type your PAT in the password input box on the right. enter image description here

When you call this REST API from powershell, the response returns an error: "TF400898: An Internal Error Occurred. Activity Id: 759888ab-9828-4cb6-ba2f-e90cde1cd39a." The cause of this problem is Content- Type is incorrect.

Please use ContentType "application/json" instead of ContentType "application/json-patch+json" .

Script template:
$token = "PAT"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$url3="https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/releases/{releaseId}/environments/{environmentId}?api-version=6.0-preview.6"
  $body = "{
  `"status`": `"inProgress`",
  `"scheduledDeploymentTime`": null,
  `"comment`": null,
  `"variables`": {}
    }"
$response3 = Invoke-RestMethod -Uri $url3 -Headers @{Authorization = "Basic $token"} -Method Patch -Body $body -ContentType application/json

Please refer to the following steps to create a PAT:

  1. From your home page, open your user settings, and then select Personal access tokens. enter image description here
  2. From your home page, open your user settings, and then select Personal access tokens. Name your token, select the organization where you want to use the token, and then choose a lifespan for your token. Select Create. enter image description here
  3. When you're done, make sure to copy the token. For your security, it won't be shown again. Use this token as your password.