Rest api error

Yadav, Venkatesh 0 Reputation points
2023-08-12T10:35:51.7933333+00:00

Hi Guys,

I am working on script in python for adding multiple tasks in workitems in azure devops using rest - api please help me on this issue.

error - Error creating task: {"$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}

code:-

import requests
import base64

def convert_to_base64(pat):
    pat_bytes = pat.encode('ascii')
    base64_pat = base64.b64encode(pat_bytes).decode('ascii')
    return base64_pat

def create_task(organization, project, base64_token, workitem, title, description):
    url = f"https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{workitem}?api-version=7.0"
    headers = {
        "Authorization": f"Basic {base64_token}",
        "Content-Type": "application/json-patch+json",
    }
    data = [
        {
            "op": "add",
            "path": "/fields/System.Title",
            "value": title,
        },
        {
            "op": "add",
            "path": "/fields/System.Description",
            "value": description,
        },
    ]

    response = requests.patch(url, headers=headers, json=data)

    if response.status_code == 200:
        print("Task created successfully!")
        print(response.json())
    else:
        print("Error creating task: " + response.text)

if __name__ == "__main__":
    organization = 'org'
    project = 'proj'
    work_item_id = id
    personal_access_token = 'token'
    title = "My task"
    description = "This is my task description."
    base64_token = convert_to_base64(personal_access_token)
    create_task(organization, project, base64_token, work_item_id, title, description)

Community Center Not monitored
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. navba-MSFT 27,540 Reputation points Microsoft Employee Moderator
    2023-08-14T02:51:30.4+00:00

    @Yadav, Venkatesh Welcome to Microsoft Q&A! Thanks for posting the question.

    The same issue has been discussed in this GitHub thread. The issue is isolated to be with the PAT token generation. Refer the last comment for the action plan.

    Also note that, DevOps is currently not supported in the Q&A forums, the supported products are listed over here https://learn.microsoft.com/en-us/answers/products (more to be added later on).

    You can ask the experts in the dedicated forum over here:
    https://stackoverflow.com/questions/tagged/devops https://developercommunity.visualstudio.com/spaces/21/index.html

    **
    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.