You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Azure DevOps爬取指定文件并下载时遭遇400错误(危险Request.Path)的问题求助

Azure DevOps爬取指定文件并下载时遭遇400错误(危险Request.Path)的问题求助

Hey there! Let's break down what's causing that frustrating 400 error and fix it right up.

What's Going Wrong?

The core issue is malformed URL construction. Your organization variable is set to the full Azure DevOps URL (https://dev.azure.com/MYORG), but when you build the file download URL, you're prepending https://dev.azure.com/ again. This creates a broken URL like:

https://dev.azure.com/https://dev.azure.com/MYORG/[your-project]/_apis/git/repositories/[repo-name]/items?path=mkdocs.yml&api-version=7.1

All those extra slashes and the : from https:// are triggering Azure DevOps' path validation—hence the "potentially dangerous Request.Path" error.

Fix 1: Fix the URL Construction

You can fix this by extracting just your organization name from the full URL, then using that in your request:

# Inside your repo loop, replace the URL line with this
org_name = organization.split('/')[-1]  # Pulls "MYORG" from "https://dev.azure.com/MYORG"
url = f'https://dev.azure.com/{org_name}/{project}/_apis/git/repositories/{repo_name}/items?path={file_path}&api-version=7.1'

Fix 2: Use the Azure DevOps SDK (Better & More Reliable)

Since you're already using the official Azure DevOps Python SDK, you can skip manual HTTP requests entirely—this avoids URL/auth mistakes and handles edge cases like content encoding automatically. Here's how to rewrite your download logic:

# Inside your repo loop, replace the entire requests.get block with this
try:
    # Fetch the file directly via the SDK (auth is already handled by your Connection)
    file_item = git_client.get_item(repository_id=repo_name, path=file_path, project=project)
    
    # Decode the base64-encoded content returned by the API
    file_content = base64.b64decode(file_item.content).decode('utf-8')
    
    # Avoid overwriting files! Create a folder per repo to store unique versions
    os.makedirs(repo_name, exist_ok=True)
    save_path = os.path.join(repo_name, file_path)
    
    with open(save_path, 'w') as local_file:
        local_file.write(file_content)
    
    print(f"✅ File '{file_path}' downloaded from repo '{repo_name}' to {save_path}")
except Exception as e:
    print(f"❌ Failed to fetch file from '{repo_name}': {str(e)}")

Quick Side Note

Right now, your original code saves every downloaded file to the same mkdocs.yml path—this means each new download will overwrite the last one. The SDK example above fixes this by creating a repo-specific folder to store each unique file version.

Final Checks

  • Double-check that your PAT has the correct permissions (you need Code (Read) access for the project/repos).
  • Verify that file_path is correct relative to the repo root (e.g., if the file is in a subfolder like docs/mkdocs.yml, update that variable accordingly).

That should get your script working smoothly! 😊

备注:内容来源于stack exchange,提问作者Devin York DJ

火山引擎 最新活动