I added an additional nuget package source to my project. I have entered my username and my password and I can install new nuget packages from the source. But I always get this error when I use the command dotnet tool restore in the Visual Studio Terminal. I use the newest Visual Studio Community 2022 (64-bit) Version on my Windows laptop.
Unhandled exception: System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized). at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at NuGet.Protocol.HttpSource.<>c__DisplayClass15_01.<b__0>d.MoveNext() --- End of stack trace from previous location --- at NuGet.Common.ConcurrencyUtilities.ExecuteWithFileLockedAsync[T](String filePath, Func2 action, CancellationToken token) at NuGet.Common.ConcurrencyUtilities.ExecuteWithFileLockedAsyncT at NuGet.Protocol.HttpSource.GetAsync[T](HttpSourceCachedRequest request, Func2 processAsync, ILogger log, CancellationToken token) at NuGet.Protocol.PackageMetadataResourceV3.LoadRegistrationIndexAsync(HttpSource httpSource, Uri registrationUri, String packageId, SourceCacheContext cacheContext, Func2 processAsync, ILogger log, CancellationToken token) at NuGet.Protocol.PackageMetadataResourceV3.GetMetadataAsync(String packageId, Boolean includePrerelease, Boolean includeUnlisted, VersionRange range, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token) at NuGet.Protocol.PackageMetadataResourceV3.GetMetadataAsync(String packageId, Boolean includePrerelease, Boolean includeUnlisted, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token) at Microsoft.DotNet.Cli.NuGetPackageDownloader.NuGetPackageDownloader.GetPackageMetadataAsync(PackageSource source, String packageIdentifier, Boolean includePrerelease, Boolean includeUnlisted, CancellationToken cancellationToken) at Microsoft.DotNet.Cli.NuGetPackageDownloader.NuGetPackageDownloader.GetPackageMetadataAsync(String packageIdentifier, NuGetVersion packageVersion, IEnumerable1 sources, CancellationToken cancellationToken, Boolean includeUnlisted) at Microsoft.DotNet.Cli.NuGetPackageDownloader.NuGetPackageDownloader.GetPackageSourceAndVersion(PackageId packageId, NuGetVersion packageVersion, PackageSourceLocation packageSourceLocation, Boolean includePreview, Boolean includeUnlisted, PackageSourceMapping packageSourceMapping) at Microsoft.DotNet.Cli.NuGetPackageDownloader.NuGetPackageDownloader.DownloadPackageAsync(PackageId packageId, NuGetVersion packageVersion, PackageSourceLocation packageSourceLocation, Boolean includePreview, Boolean includeUnlisted, Nullable1 downloadFolder, PackageSourceMapping packageSourceMapping) at Microsoft.DotNet.Cli.ToolPackage.ToolPackageDownloader.DownloadAndExtractPackage(PackageLocation packageLocation, PackageId packageId, INuGetPackageDownloader nugetPackageDownloader, String packagesRootPath, IToolPackageStore toolPackageStore, NuGetVersion packageVersion, PackageSourceLocation packageSourceLocation, Boolean includeUnlisted) at Microsoft.DotNet.Cli.ToolPackage.ToolPackageDownloader.<>c__DisplayClass8_0.b__0() at Microsoft.DotNet.Cli.TransactionalAction.RunT at Microsoft.DotNet.Tools.Tool.Restore.ToolRestoreCommand.InstallPackages(ToolManifestPackage package, Nullable1 configFile) at Microsoft.DotNet.Tools.Tool.Restore.ToolRestoreCommand.<>c__DisplayClass10_0.b__0(ToolManifestPackage package) at System.Linq.Enumerable.SelectArrayIterator2.Fill(ReadOnlySpan1 source, Span1 destination, Func2 func) at System.Linq.Enumerable.SelectArrayIterator2.ToArray() at System.Linq.Enumerable.ToArrayTSource at Microsoft.DotNet.Tools.Tool.Restore.ToolRestoreCommand.Execute() at System.CommandLine.Invocation.InvocationPipeline.Invoke(ParseResult parseResult) at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, TimeSpan startupTime, ITelemetry telemetryClient)
In addition, I have tried the commands dotnet nuget locals all --clear and after that dotnet restore in Terminal, but it didn't solved the problem.
Then I have added the following nuget.config file to my project but I still get the same error:
<?xml version="1.0" encoding="utf-8"?><configuration><packageSources><clear /><add key="monogame development" value="https://nuget.pkg.github.com/MONOGAME/index.json" /></packageSources><packageSourceCredentials><github> <add key="Username" value="MyUsername" /><add key="ClearTextPassword" value="MyPersonalAccessToken" /></github></packageSourceCredentials></configuration>
In addition, I use the same NuGet package source, username and password in a project in Visual Studio Code on my MacBook Air and there it works. I have tried to copy this project from my Mac to my Windows laptop but then I get this error when I open the project in Visual Studio Code on Windows:
C:\Program Files\dotnet\sdk\8.0.300\NuGet.targets(169,5): warning :Your request could not be authenticated by the GitHub Packagesservice. Please ensure your access token is valid and has theappropriate scopes configured.[C:\Users\name\Documents\andproj\andproj\andproj.sln] Retrying'FindPackagesByIdAsync' for source'https://nuget.pkg.github.com/MonoGame/download/monogame.framework.android/index.json'.Response status code does not indicate success: 401 (Unauthorized).
How can I solve the 401 (Unauthorized) error?