dotnet nuget push推送NuGet包到GitHub Packages报400错误如何解决
GitHub NuGet包推送返回400错误排查方案
问题背景
- 三周前可正常向所属GitHub组织的Packages仓库推送NuGet包,当时使用的推送命令:
dotnet nuget push "<fullNameOfPackage.nupkg>" --source "<Our-Org>"
- 前期配置:已创建个人访问令牌(PAT),通过以下命令将令牌添加到nuget.config配置中:
dotnet nuget add source "https://nuget.pkg.github.com/<Our-Org>/index.json" --name "<Our-Org>" --username "<my-username>" --password "<my-token>" --store-password-in-clear-text
- 额外备份:本地项目目录存放了一份nuget.config副本。
- 故障表现:原有配置三周前可正常运行,推送新版本包时返回如下错误:
PS G:\_dev\solution\project> dotnet nuget push bin\Debug\<fullNameOfPackage.nupkg> --source "<Our-Org>" warn : No API Key was provided and no API Key could be found for 'https://nuget.pkg.github.com/<Our-Org>'. To save an API Key for a source use the 'setApiKey' command. Pushing <fullNameOfPackage.nupkg> to 'https://nuget.pkg.github.com/<Our-Org>'... PUT https://nuget.pkg.github.com/<Our-Org>/ warn : invalid repo host 'nuget.pkg.github.com', only github (github.com) repositories allowed BadRequest https://nuget.pkg.github.com/<Our-Org>/ 1079ms error: Response status code does not indicate success: 400 (Bad Request).
- 已尝试操作:重新生成PAT、使用curl命令推送、通过NuGet Package Explorer上传包文件,所有方式均推送失败,始终返回400错误。
解决步骤
清理失效旧源配置
删除全局nuget.config(默认路径为用户目录下.nuget/NuGet/NuGet.Config)和项目目录下副本nuget.config中,之前添加的指向nuget.pkg.github.com/<Our-Org>的自定义源条目,避免旧配置优先级冲突。重新添加符合最新鉴权规则的源
执行以下命令重新添加源,注意源地址不要省略前缀、不要用自定义映射名替代完整路径:
dotnet nuget add source "https://github.com/orgs/<Our-Org>/packages" --name "<Our-Org>" --username "<your-github-username>" --password "<your-valid-PAT>" --store-password-in-clear-text
PAT必须勾选read:packages、write:packages、repo三个权限,若组织开启了PAT访问审批,需要提前找组织管理员完成PAT授权。
- 配置对应源的API Key
执行命令将PAT设置为该源的API Key,消除命令行中的API Key缺失警告:
dotnet nuget setapikey <your-valid-PAT> --source "<Our-Org>"
- 重新执行推送
推送时可以直接指定源名称,也可以写完整源地址:
dotnet nuget push bin\Debug\<fullNameOfPackage.nupkg> --source "<Our-Org>" --skip-duplicate
临时验证方案:如果不想修改全局配置,可以在推送命令里直接带上api-key参数和完整源地址,跳过本地配置读取,命令如下:
dotnet nuget push <nupkg文件全路径> --source "https://nuget.pkg.github.com/<Our-Org>/index.json" --api-key <your-PAT>该方案可直接验证是否为本地配置冲突导致的故障。
内容的提问来源于stack exchange,提问作者Mike
相关产品推荐
相关产品推荐

