Quantcast
Channel: Active questions tagged nuget-package - Stack Overflow
Viewing all articles
Browse latest Browse all 3067

NuGet Client SDK - Connecting to a private NuGet feed through code

$
0
0

I'm using a private NuGet feed through Azure DevOps.
To see information about a public NuGet package from code, one simply has to follow the documentation here. In particular:

ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;

SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
PackageMetadataResource resource = await repository.GetResourceAsync<PackageMetadataResource>();

IEnumerable<IPackageSearchMetadata> packages = await resource.GetMetadataAsync(
    "Newtonsoft.Json",
    includePrerelease: true,
    includeUnlisted: false,
    cache,
    logger,

I'm trying to achieve the same result for my own private feed by adding my credentials to the process:

var logger = NullLogger.Instance;
var cancellationToken = CancellationToken.None;

var cache = new SourceCacheContext();    
var source = new PackageSource("*my feed*/nuget/v3/index.json");
source.Credentials = new PackageSourceCredential("*my feed*/nuget/v3/index.json", "*username*", "*password*", true, null);

var repository = Repository.Factory.GetCoreV2(source);
var resource = await repository.GetResourceAsync<PackageMetadataResource>();

IEnumerable<IPackageSearchMetadata> searchMetadata = await resource.GetMetadataAsync(
    "My.Package.Name",
    includePrerelease: true,
    includeUnlisted: false,
    cache,
    logger,
    cancellationToken);

Please note, that even though I call the Factory.GetCoreV2(), the inside of the method still calls V3 providers. It's simply the only constructor that takes a PackageSource as a parameter:

public static SourceRepository GetCoreV2(this Repository.RepositoryFactory factory, PackageSource source)
{
     return Repository.CreateSource(Repository.Provider.GetCoreV3(), source);
}

However, no matter what I do, I cannot get through step

var resource = await repository.GetResourceAsync<PackageMetadataResource>();

which keeps failing with a NuGet.Protocol.Core.Types.FatalProtocolException - 401 Unauthorised. I've verified the validity of my credentials and my feed endpoint, as well as my permissions within the feed (full access). I've also tried the initialisation process described here (plus the step with my credentials), yet the result is the same.

How am I expected to identify to an Azure DevOps feed from code, to be able to get the IPackageMetadata information?


Viewing all articles
Browse latest Browse all 3067

Trending Articles