Search code examples
nugetvisual-studio-2019nuget-packagegithub-package-registry

How to use github nuget packages on Visual Studio 2019


I try to learn Github action and packages. So, I create a sample nuget package and successfully created. However I could not use it.

I follow this step;

  1. Options > Nuget Package Manager > Package Sources
  2. Click add button write organization name and add package source address like that: enter image description here

  3. After that Visual Studio doesn't ask me any credential for this address.

  4. If there is no credential, I will expect to an error. And I got it.

    [github] Failed to retrieve metadata from source 'https://nuget.pkg.github.com/[OrganizationName]/query?q=&skip=0&take=26&prerelease=false&semVerLevel=2.0.0'. Response status code does not indicate success: 401 (Unauthorized).

  5. Then, I decide to some credential like explained in github documentation.

  6. My nuget.config file: enter image description here

  7. However still I get Unauthorized error. And also when I click the error link gettin a message like that:

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

but I already have a token with read/write package credential. enter image description here

  1. All that fails and I try to another way. I add credential information to Windows Credential Manager. Eveything still same.

So, how I add and use my private nuget packages?


Solution

  • How to use github nuget packages on Visual Studio 2019

    First, make sure that your credential info is correct on the nuget.config file.

    I found your document suggest you should put a new nuget.config file to your solution. This nuget.config file is a local action file, it will act on any projects in the current subdirectory and below the current level. You can refer to this document.

    In this way, you should restart VS Instance to enable this new nuget.config file.

    -------Global nuget.config--------

    However, from your description and first picture, you used the global nuget.config file. And UI's info stores under global nuget.config file.

    If you want to config this gihub package source for all the projects on your PC, you should config it on the global nuget.config file.

    And the first picture which you provided indicates that you used in the global file(C:\Users\xxx(current user)\AppData\Roaming\NuGet\NuGet.Config).

    This function also needs restart VS to enable the new nuget.config file.

    Suggestion

    1. please add your content of the file into C:\Users\xxx(current user)\AppData\Roaming\NuGet\NuGet.Config.

    2. restart VS Instance or restart PC to enable this new nuget.config file. It is designed by that.