如何使用Visual Studio 2019设置私有NuGet包注册表?

4
我正在尝试通过NuGet包管理器在Visual Studio 2019中提供一个.NET Framework v4.5.2包,并从Github的私有NuGet包注册系统获取。
我使用的重要工具有:
- .NET Framework 4.5.2 - Github Actions - Github - Github Package Registry (GPR) - Visual Studio 2019/NuGet包管理器
我的目标是什么?
整个生命周期看起来像这样:
  1. 我的项目仓库有一个CI任务 (Github 品牌) 用于 msbuild & 发布 nupkg。它将所有内容包装起来并将其部署到 https://nuget.pkg.github.com/< my-private-org >。
  2. 在 Visual Studio 2019 中,用户可以打开给定项目,并参考 https://nuget.pkg.github.com/< my-private-org > 作为自定义 NuGet feed,并将 "My Project" 声明为依赖项。

什么有效,什么无效?

[Private] Failed to retrieve metadata from source 'https://nuget.pkg.github.com/<my-private-organization>/query?q=&skip=0&take=26&prerelease=true&supportedFramework=.NETFramework,Version=v4.7&semVerLevel=2.0.0'.
  Response status code does not indicate success: 401 (Unauthorized).

当我尝试在浏览器中运行相同的查询(GET https://nuget.pkg.github.com/<my-private-organization>/query?q=&skip=0&take=26&prerelease=true&supportedFramework=.NETFramework,Version=v4.7&semVerLevel=2.0.0')时,会弹出一个模态框要求输入GitHub凭据,但是在输入后,我遇到了相同的401错误,但是有不同的错误消息。
{
  "errors": [
    { 
      "code" : "Your token has not been granted the required scopes to execute this query. The 'id' field requires one of the following scopes",
      "message":" ['read:packages'], but your token has only been granted the: [''] scopes. Please modify your token's scopes at: https://github.com/settings/tokens."
    }
  ]
}

我肯定拥有一个具有read:package权限的GITHUB_TOKEN——我在我的CI作业中使用它来生成包。

我有怀疑...

我需要将我的read:package凭证的GITHUB_TOKEN作为额外的认证凭据传递,但我不知道如何在nuget.config中配置。这就导致了...

我的配置是什么?

我读到需要使用一些额外的凭证信息对我的%APPDATA%/NuGet/nuget.config进行参数化,这些信息来自于这里:https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file#packagesourcecredentials。我可以确认我正在更新正确的nuget.config文件——我已经能够使用我正在修改的文件更改Visual Studio中的软件包流名称。现在它看起来有点像:

<!-- file: nuget.config -->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Private" value="https://nuget.pkg.github.com/###############/index.json" />
    <add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\" />
  </packageSources>
  <packageSourceCredentials>
    <Private>
        <add key="Username" value="###############" />
        <add key="ClearTextPassword" value="*******************" />
        <!-- Could I perhaps need to introduce my Github Access Token here? -->
    </Private>
  </packageSourceCredentials>
</configuration>

packageSourceCredentials 中指定的用户名和密码与我在浏览器中手动进行的 GET 查询中使用的相同(该查询因 401 而失败)。

欢迎所有想法,甚至是“没人做A”。

1个回答

6
为了使用 Github 包源(Github package feed)上的 NuGet 包,您需要使用 GitHub 个人访问令牌(personal access token),而不是您的 Github 用户名和密码。
请阅读官方文档: https://docs.github.com/en/free-pro-team@latest/packages/using-github-packages-with-your-projects-ecosystem/configuring-dotnet-cli-for-use-with-github-packages#authenticating-with-a-personal-access-token 使用这份指南创建个人访问令牌: https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token

Eriawan,恭敬地请问您能解释一下个人访问令牌和GitHub令牌之间的区别吗?谢谢答复。 - Ian Ray
我的错误,我应该澄清“不应该使用用户ID和密码”。我会更新我的回答。 - Eriawan Kusumawardhono
太棒了。这确实解决了问题@Erianwan。再次感谢!我发誓我已经尝试过这个了。¯_(ツ)_/¯ - Ian Ray

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接