You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Cargo CLI发布包至JFrog Artifactory私有仓库遇401未授权求助

Cargo Publish到JFrog Artifactory返回401 Unauthorized问题排查

问题详情

执行cargo publish向JFrog Artifactory联合Cargo仓库发布包时,返回401未授权错误,具体错误输出:

error: failed to publish to registry at https://<ARTIFACTORY_URL>>/artifactory/api/cargo/cargo-repo

Caused by: failed to get a 200 OK response, got 401
headers:HTTP/2 401
date: Wed, 11 Nov 2024 20:41:49 GMT
content-type: application/json
x-artifactory-trace-id:
x-jfrog-version:
x-artifactory-id:
x-artifactory-node-id:

body:Unauthorized user

当前Cargo配置文件(~/.cargo/config.toml)内容:

[registry]
default = "artifactory"
global-credential-providers = ["cargo:token"]

[registries.artifactory] 
index = "sparse+https://<ARTIFACTORY_URL>/artifactory/api/cargo/cargo-repo-name/index/"

凭证文件(~/.cargo/credentials.toml)内容:

[registries.artifactory] token = "Bearer <TOKEN>

排查与解决方案

  • 修复凭证文件语法错误:
    凭证文件中token值的闭合引号缺失,导致Cargo无法正确解析令牌。修正后应为:

    [registries.artifactory] token = "Bearer <TOKEN>"
    
  • 验证令牌权限与有效性:

    1. 确认生成的Artifactory令牌关联的用户,对目标联合Cargo仓库拥有Deploy权限(需同时检查联合仓库包含的底层本地仓库权限)。
    2. 用curl直接验证令牌有效性:
      curl -H "Authorization: Bearer <TOKEN>" https://<ARTIFACTORY_URL>/artifactory/api/cargo/cargo-repo/v1/versions
      
      若返回401,说明令牌无效或权限不足,需重新生成带正确权限的令牌;若返回200,则问题出在Cargo配置。
  • 统一仓库URL配置:
    错误信息中的仓库URL存在多余的>符号(https://<ARTIFACTORY_URL>>/...),需修正。同时,在config.toml的registries.artifactory节点添加url字段,明确发布目标地址(与联合仓库的API地址一致):

    [registries.artifactory] 
    index = "sparse+https://<ARTIFACTORY_URL>/artifactory/api/cargo/cargo-repo-name/index/"
    url = "https://<ARTIFACTORY_URL>/artifactory/api/cargo/cargo-repo/"
    

    确保url中的仓库名称与Artifactory中创建的联合仓库名称完全一致。

  • 检查凭证文件权限:
    Linux/macOS下需确保credentials.toml的权限为600(仅当前用户可读可写),避免Cargo因权限问题拒绝加载凭证:

    chmod 600 ~/.cargo/credentials.toml
    
  • 确认稀疏索引支持:
    确保Artifactory的目标Cargo仓库已启用稀疏索引功能,且index URL中的路径与仓库配置的索引路径一致。

内容的提问来源于stack exchange,提问作者Hansraj Mathakar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 06:05:58