8

I'm trying to publish a NuGet package to a private VSTS feed. I'd like to achieve that using only .NET CLI and without creating or modifying a nuget.config file.

I've tried to do:

dotnet nuget push <PackageName> --source https://XXX.pkgs.visualstudio.com/_packaging/YYY/nuget/v3/index.json --api-key <VSTS UserName>:<PersonalAccessToken>

I get:

error: Unable to load the service index for source https://XXX.pkgs.visualstudio.com/_packaging/YYY/nuget/v3/index.json.
error: Response status code does not indicate success: 401 (Unauthorized).

I can see in Fiddler that .NET CLI sends only a GET request to https://XXX.pkgs.visualstudio.com/_packaging/YYY/nuget/v3/index.json without any authorization token. That request ends with 401.

mfluehr
  • 2,832
  • 2
  • 23
  • 31
Jakub Binkowski
  • 553
  • 1
  • 5
  • 10

1 Answers1

16

The NuGet package credential and API key should be added in the NuGet.config file.

So before using the dotnet nuget push command, you should add the credential and API key in NuGet.config as below two commands:

nuget sources Add -Name "mysource" -Source "https://XXX.pkgs.visualstudio.com/_packaging/YYY/nuget/v3/index.json" -username name -password PAT
nuget setapikey mykey -source mysource

Then push the NuGet package through the dotnet nuget push command:

dotnet nuget push packagename.nupkg --source mysource --api-key mykey
Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • 1
    Thanks. I was looking for an option that: a) doesn't require to store the keys in NuGet config as I wanted to check in the file to repository b) doesn't require nuget.exe But I guess, it can't be done (yet?) – Jakub Binkowski Jan 04 '18 at 06:49
  • Yeah, it can not be done. The credentials should be searched in nuget.config file as the description https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-push says: "The `dotnet nuget push` command pushes a package to the server and publishes it. The push command uses server and credential details found in the system's NuGet config file or chain of config files. " – Marina Liu Jan 04 '18 at 06:55
  • 5
    Note that you won't need either the setapikey line or the --api-key parameter when pushing to VSTS Package Management. Otherwise though, the answer is correct. You can manually construct a NuGet.config without using nuget.exe, if you'd like. There's a reference here that can help: https://learn.microsoft.com/en-us/nuget/schema/nuget-config-file#package-source-sections – Alex Mullans Jan 04 '18 at 21:56
  • 2
    You have used `mykey` and `PAT`. I suppose they do not represent the same value. How then can I generate this `mykey`? – Andre Soares Feb 14 '18 at 12:48
  • 1
    @Andre Soares: Yes, mykey and PAT do not represent the same value: "The NuGet client's push command requires an API key. [With VSTS Package Server] You can use any non-empty string you want". Source: https://learn.microsoft.com/en-us/vsts/package/nuget/publish?view=vsts – KimCM May 17 '18 at 13:26
  • @MarinaLiu-MSFT is `nuget setapikey mykey` needed? when you use `--api-key mykey` – Konrad Jun 27 '18 at 11:59
  • "Saves an API key for a given server URL into NuGet.Config so that it doesn't need to be entered for subsequent commands." – Konrad Jun 27 '18 at 12:02
  • It would mean that I don't need to use `--api-key mykey` then – Konrad Jun 27 '18 at 12:02
  • oh nevermind I didn't read all comments, it was answered by Alex – Konrad Jun 27 '18 at 12:10
  • I'm convinced none of microsofts shitty vsts tasks work for nuget. Every avenue we try there are issues. – christopher clark Sep 14 '18 at 18:58
  • In my case had to push a package to GitHub Package Registry so did: `nuget setapikey -source github`. It added some gibberish to `%appdata%\NuGet\NuGet.Config` and then could push the package. – Ricardo stands with Ukraine Mar 11 '21 at 12:08