Getting authorization issue when trying to update azure work item with patch request using rest assured with java

Viewed 20

I am trying below code for updating work item in azure, getting error -

{"$id":"1","innerException":null,"message":"TF400813: The user 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' is not authorized to access this resource.","typeName":"Microsoft.TeamFoundation.Framework.Server.UnauthorizedRequestException, Microsoft.TeamFoundation.Framework.Server","typeKey":"UnauthorizedRequestException","errorCode":0,"eventId":3000} Exception in thread "main" java.lang.AssertionError: 1 expectation failed. Expected status code <200> but was <401>.

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;

public class testee {
public static void main(String[] args) throws Exception {
    String url = "https://dev.azure.com/{organization}/{Project}/_apis/wit/workitems/{ID}?api-version=7.1-preview.3";

    String jsonString = "[{\"op\":\"replace\",\"path\":\"/fields/System.Title\",\"value\":\"Test - Title\"},{\"op\":\"add\",\"path\":\"/fields/System.State\",\"value\":\"Active\"}]";

    // Create a request specification
    RequestSpecification request = RestAssured.given();

    // Setting content type to specify format in which request payload will be sent.
    // ContentType is an ENUM.
    request.contentType(ContentType.JSON);
    // Setting a cookie for authentication as per API documentation
    
    request.cookie("token", "{token_id}");

    // Adding URI
    request.baseUri(url);
    // Adding body as string
    request.body(jsonString);

    // Calling PATCH method on URI. After hitting we get Response
    Response response = request.patch();

    // Printing Response as string
    System.out.println(response.asString());

    // Get Validatable response to perform validation
    ValidatableResponse validatableResponse = response.then();

    // Validate status code as 200
    validatableResponse.statusCode(200);

}
}
0 Answers
Related