4

I am attempting to make available a .NET Framework v4.5.2 package in Visual Studio 2019, via NuGet Package Manager, and sourced from Github's private NuGet package registry system.

Important Stuff I'm Using:

  • .NET Framework 4.5.2
  • Github Actions
  • Github
  • Github Package Registry (GPR)
  • Visual Studio 2019/NuGet Package Manager

What Is My Goal?

The whole lifecycle looks a bit like this:

  1. My project's repo has a CI job (Github brand) to msbuild & publish nupkg. It wraps everything up and deploys it to https://nuget.pkg.github.com/< my-private-org >.
  2. In Visual Studio 2019, with a given project cracked open, a user should be able to reference https://nuget.pkg.github.com/< my-private-org > as a custom NuGet feed, and declare "My Project" as a dependency.

What Works, What Doesn't?

[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).

When I attempt to run the same query (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') in the browser, I am prompted with a modal to enter my GitHub credentials, but after entering them, I am met with the same 401, but a different error message:

{
  "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."
    }
  ]
}

I definitely have a GITHUB_TOKEN with read:package perms--I used it to generate the package in the first place in my CI job.

I am suspicious that...

I need to pass my read:package credentialed GITHUB_TOKEN as extra auth-credentials, but I don't know if/how I can configure that in nuget.config. Which leads us to...

What is my configuration?

I read that I need to parameterize my %APPDATA%/NuGet/nuget.config with some extra credential information, which I got from here: https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file#packagesourcecredentials. I can confirm that I'm updating the correct nuget.config file--I've been able to change package stream names in Visual Studio with the file I'm touching. It now looks a bit like:

<!-- 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>

The username and password specified in packageSourceCredentials are the same I used in the manual GET query done in the browser (which failed in a 401).

All thoughts are welcome, even "Nobody Does A".

Ian Ray
  • 378
  • 2
  • 18

1 Answers1

6

In order to consume the nuget from Github package feed, you have to use GitHub personal access token, not your Github username and password.

Read the official doc on 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

Use this guide to create personal access token: https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token

Eriawan Kusumawardhono
  • 4,796
  • 4
  • 46
  • 49