如何使用python脚本获取Azure DevOps客户端

如何使用python脚本获取Azure DevOps客户端,python,azure-devops,devops,Python,Azure Devops,Devops,我正在尝试使用python脚本获取Azure DevOps的客户端。 以下是我为此目的使用的代码: from azure.devops.connection import Connection git_client = p_context.connection.clients.get_git_client() p_上下文是我的Azure DevOps组织的访问令牌 以下是我面临的错误: Traceback (most recent call last): File "/home/vs

我正在尝试使用python脚本获取Azure DevOps的客户端。 以下是我为此目的使用的代码:

from azure.devops.connection import Connection
   git_client = p_context.connection.clients.get_git_client()
p_上下文是我的Azure DevOps组织的访问令牌

以下是我面临的错误:

Traceback (most recent call last):
  File "/home/vsts/work/1/s/IdentifyGitBuildCommitItems.py", line 95, in <module>
    t = get_commit_changes_for_build(context ,azdevops_projectid ,azdevops_build_id ,azdevops_build_number ,bld_commit.id ,azdevops_repository_name)
  File "/home/vsts/work/1/s/IdentifyGitBuildCommitItems.py", line 51, in get_commit_changes_for_build
    comt = git_client.get_changes(p_commit_id ,repository_id=p_repository_name ,project=p_project_id)
  File "/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site-packages/azure/devops/released/git/git_client_base.py", line 250, in get_changes
    query_parameters=query_parameters)
  File "/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site-packages/azure/devops/client.py", line 104, in _send
    response = self._send_request(request=request, headers=headers, content=content, media_type=media_type)
  File "/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site-packages/azure/devops/client.py", line 68, in _send_request
    self._handle_error(request, response)
  File "/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site-packages/azure/devops/client.py", line 295, in _handle_error
    status_code=response.status_code))
azure.devops.exceptions.AzureDevOpsClientRequestError: Page not found.  Operation returned a 404 status code.
##[error]/opt/hostedtoolcache/Python/3.6.10/x64/bin/python failed with return code: 1
回溯(最近一次呼叫最后一次):
文件“/home/vsts/work/1/s/identifyGitBuildCommitteems.py”,第95行,在
t=获取生成的更改(上下文、azdevops项目、azdevops生成id、azdevops生成编号、bld提交id、azdevops存储库名称)
文件“/home/vsts/work/1/s/identifyGitBuildCommitteems.py”,第51行,在get_commit_changes_for_build中
comt=git\u client.get\u更改(p\u commit\u id,repository\u id=p\u repository\u name,project=p\u project\u id)
文件“/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site packages/azure/devops/released/git/git_client_base.py”,第250行,在get_changes中
查询参数=查询参数)
文件“/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site packages/azure/devops/client.py”,第104行,在
响应=自我。\发送\请求(请求=请求,标题=标题,内容=内容,媒体类型=媒体类型)
文件“/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site packages/azure/devops/client.py”,第68行,在发送请求中
自我处理错误(请求、响应)
文件“/opt/hostedtoolcache/Python/3.6.10/x64/lib/python3.6/site packages/azure/devops/client.py”,第295行,在“句柄”中出错
状态(代码=响应。状态(代码))
azure.devops.exceptions.AzureDevOpsClientRequestError:未找到页面。操作返回404状态代码。
##[错误]/opt/hostedtoolcache/Python/3.6.10/x64/bin/Python失败,返回代码:1
这是我在我的用例中遵循的完整GitHub示例:

您可以检查并尝试以下代码:

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
import pprint

# Fill in with your personal access token and org URL
personal_access_token = 'YOURPAT'
organization_url = 'https://dev.azure.com/YOURORG'

# Create a connection to the org
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

# Get a client 
git_client = connection.clients.get_git_client()

我已经解决了问题并解决了错误。
错误提示,因为我的源代码存在于我的个人GitHub帐户中,而不在Azure Git存储库中。当我将源代码从个人Git移动到Azure Git存储库时,错误得到解决

很高兴看到你的问题得到了解决,你可以,这对阅读此帖子的其他社区成员是有益的。