为包添加 PackageReference 失败并显示 401未经授权

问题描述

我正在尝试将第三方 NuGet 包(例如 Stylecop)安装到项目中。该项目还有从我们自己的私有 NuGet 源安装的包。在尝试安装该软件包时,我们收到了这样的 401 错误

MyProject> dotnet add .\MyProject.csproj package Stylecop
  Determining projects to restore...
  Writing C:\Users\me\AppData\Local\Temp\tmp6A51.tmp
info : Adding packagereference for package 'Stylecop' into project '.\MyProject.csproj'.
info :   CACHE https://api.nuget.org/v3/registration5-gz-semver2/stylecop/index.json
info :   GET https://mediavaletsc.pkgs.visualstudio.com/_packaging/c559332f-5d8d-4d3f-8fc2-6e06125775fa/nuget/v3/registrations2-semver2/stylecop/index.json
info :   Unauthorized https://mycompany.pkgs.visualstudio.com/_packaging/xxx-xxx-xxx-xxx-xxx/nuget/v3/registrations2-semver2/stylecop/index.json 680ms
error: Response status code does not indicate success: 401 (Unauthorized).

奇怪的是,即使在执行 dotnet restore 之后,运行 git clean -xfd 也能正常工作。我们如何配置 dotnet 和/或 nuget 以接受 Stylecop 或任何其他公共软件包的安装?

解决方法

我们需要使用 Visual Studio Team Services (VSTS) 重新进行身份验证,因为 API 密钥已过期和/或被撤销。

dotnet restore 仍然有效,但 dotnet add ... package ... 失败的原因是前者使用了本地缓存,而后者命中了服务器。运行 nuget locals all -clear 后,恢复也停止工作。

要重新进行身份验证,请执行以下操作。

nuget sources remove -name $sourceName | Out-Null;
nuget sources add -name $sourceName -source $sourcePath -username $username -password $apiKey

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...