简体   繁体   English

尝试连接到 Azure DevOps 工件时“无法验证”通过 npm 馈送; 我收到 E401 错误

[英]'Unable to Authenticate' when trying to connect to Azure DevOps Artifacts feed through npm; I get an E401 error

I'm trying to connect to a Azure DevOps Artifacts feed, but I keep getting an E401 error.我正在尝试连接到 Azure DevOps Artifacts 提要,但我不断收到 E401 错误。

I've tried on a different computer and it connected just fine.我在另一台计算机上试过,它连接得很好。 I've uninstalled, and reinstalled my Node.js, and I still got the error.我已经卸载并重新安装了我的 Node.js,但仍然出现错误。

Below is the error I get when I run 'npm install'以下是我在运行“npm install”时遇到的错误

npm ERR! code E401
npm ERR! Unable to authenticate, need: Bearer authorization_uri=https://login.windows.net/*********, Basic realm="https://pkgsprodcus1.pkgs.visualstudio.com/", TFS-Federated

Below is what was added to the.npmrc file以下是添加到 .npmrc 文件中的内容

registry=https://[org].pkgs.visualstudio.com/_packaging/[feed].Npm/npm/registry
//pkgs.dev.azure.com/[org]/_packaging/[feed].Npm/npm/registry/:_authToken=[token]
//pkgs.dev.azure.com/[org]/_packaging/[feed].Npm/npm/:_authToken=[token]

I expected to be connected to the feed after adding my info to the.npmrc file, and running 'npm install'我希望在将我的信息添加到 .npmrc 文件并运行“npm install”后连接到提要

I had an issue where I couldn't connect, even though I had the same .npmrc as other repos on the same machine.我遇到了无法连接的问题,即使我在同一台机器上拥有与其他存储库相同的 .npmrc。 Running vsts-npm-auth -config .npmrc just exited, presumably happy with the cached credentials.运行vsts-npm-auth -config .npmrc刚刚退出,大概对缓存的凭据感到满意。

However, the credentials it had seemed to be bad.然而,它的凭据似乎很糟糕。 The solution was to force refreshing the token: vsts-npm-auth -config .npmrc -force解决方案是强制刷新令牌: vsts-npm-auth -config .npmrc -force

This usually happens when you've recently changed your password.这通常发生在您最近更改密码时。 To fix this problem, I ran this command within VS Code's Powershell terminal (any terminal will work)为了解决这个问题,我在 VS Code 的 Powershell 终端中运行了这个命令(任何终端都可以工作)

vsts-npm-auth -config .npmrc

With doing so, a GUI popped-up where I was able to select the account I wanted to use to authenticate.这样做后,会弹出一个 GUI,我可以在其中选择我想用来进行身份验证的帐户。

I hope this helps!我希望这有帮助!

When Microsoft updated changed the name of VSTS to Azure DevOps, they also changed many of the URLs.当微软更新将 VSTS 的名称更改为 Azure DevOps 时,他们也更改了许多 URL。 Most of the old ones still redirect, but it looks like you have a mismatch in your .npmrc file above.大多数旧的仍然重定向,但上面的.npmrc文件看起来不匹配。 Change the URI in the first line to match the other lines, so it looks like this:更改第一行中的 URI 以匹配其他行,如下所示:

registry=https://pkgs.dev.azure.com/[org]/_packaging/[feed].Npm/npm/registry

That should unblock you.那应该解锁你。

As a note, if you have both the registry and your credentials in the same file, and that file is being committed to source control, you are also saving your credentials to source control.请注意,如果您在同一个文件中同时拥有注册表凭据,并且该文件正在提交给源代码管理,则您还将凭据保存到源代码管理。 Best practice here is to keep .npmrc in your project root that only has that first line, the one that tells npm which registry to connect to.这里的最佳实践是将.npmrc保留在您的项目根目录中,该文件只有第一行,该行告诉 npm 要连接到哪个注册表。 Then, create a new file at ~/.npmrc (if you are using Windows you can use git bash to help put it in the right place) and just add the other two lines, the ones with your credentials.然后,在~/.npmrc创建一个新文件(如果您使用的是 Windows,您可以使用 git bash 帮助将其放在正确的位置)并添加另外两行,即带有您的凭据的行。 This will keep your creds local to your machine and the file with the registry note can safely be committed.这将使您的凭据保持在您的机器本地,并且可以安全地提交带有注册表注释的文件。

Also, remember that the credentials you generate from the Azure package registry are good for 90 days at the most, so at some point you will get the unauthorized error message again and you will need to update the credentials in your local ~/.npmrc file.另外,请记住,您从 Azure 包注册表生成的凭据最多可使用 90 天,因此在某些时候您将再次收到未经授权的错误消息,您需要更新本地~/.npmrc文件中的凭据.

I just experienced this gotcha with the Azure DevOps portal:我刚刚在 Azure DevOps 门户中遇到了这个问题:

One of my colleagues couldn't connect.我的一位同事无法连接。 It turned out that the Azure DevOps portal inserted the text %40Local after the feed name if you copy the feed URL in the browser as I did when sharing it over MS Teams.事实证明,如果您在浏览器中复制提要 URL,就像我在 MS Teams 上共享它时所做的那样,Azure DevOps 门户%40Local在提要名称后插入文本%40Local

What my portal showed:我的门户显示的内容:

; begin auth token
//pkgs.dev.azure.com/<company>/_packaging/<feed>/npm/registry/:username=xxx
//pkgs.dev.azure.com/<company>/_packaging/<feed>/npm/registry/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
//pkgs.dev.azure.com/<company>/_packaging/<feed>/npm/registry/:email=npm requires email to be set but doesn't use the value
//pkgs.dev.azure.com/<company>/_packaging/<feed>/npm/:username=xxx
//pkgs.dev.azure.com/<company>/_packaging/<feed>/npm/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
//pkgs.dev.azure.com/<company>/_packaging/<feed>/npm/:email=npm requires email to be set but doesn't use the value
; end auth token

What his portal showed:他的门户网站显示的内容:

; begin auth token
//pkgs.dev.azure.com/<company>/_packaging/<feed>%40Local/npm/registry/:username=xxx
//pkgs.dev.azure.com/<company>/_packaging/<feed>%40Local/npm/registry/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
//pkgs.dev.azure.com/<company>/_packaging/<feed>%40Local/npm/registry/:email=npm requires email to be set but doesn't use the value
//pkgs.dev.azure.com/<company>/_packaging/<feed>%40Local/npm/:username=xxx
//pkgs.dev.azure.com/<company>/_packaging/<feed>%40Local/npm/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
//pkgs.dev.azure.com/<company>/_packaging/<feed>%40Local/npm/:email=npm requires email to be set but doesn't use the value
; end auth token

That caused a mismatch with the repository URL in the projects .npmrc这导致与项目.npmrc的存储库 URL 不匹配

To fix it: Make sure the URL is correct and is matching in both user .npmrc and project .npmrc修复它:确保 URL 正确并且在用户.npmrc和项目.npmrc

https://docs.microsoft.com/en-us/azure/devops/artifacts/npm/npmrc?view=azure-devops&tabs=windows This solution works for me. https://docs.microsoft.com/en-us/azure/devops/artifacts/npm/npmrc?view=azure-devops&tabs=windows这个解决方案对我有用。 There is different PowerShell Extension which needs to install before running the command vsts-npm-auth -config .npmrc .在运行命令vsts-npm-auth -config .npmrc之前需要安装不同的 PowerShell 扩展。

My issue was caused by the fact that running vsts-npm-auth command wrote the URLs for username and password into my .npmrc file at %USERPROFILE%\\.npmrc that were missing the last URL segment /registry .我的问题是由于运行vsts-npm-auth命令将用户名和密码的 URL 写入我的 .npmrc 文件中的%USERPROFILE%\\.npmrc缺少最后一个 URL 段/registry I added the /registry segment to my user .npmrc file and all is well again.我将/registry段添加到我的用户 .npmrc 文件中,一切又好了。

None of the many answers I've seen worked for me.我见过的许多答案都不适合我。 Here's what did work for me:这对我有用:

If you haven't already then run: npm i -g vsts-npm-auth<\/code>如果您还没有运行: npm i -g vsts-npm-auth<\/code>
Open Powershell and run: vsts-npm-auth -config .npmrc -T $HOME\/.npmrc<\/code>打开 Powershell 并运行: vsts-npm-auth -config .npmrc -T $HOME\/.npmrc<\/code>

Those 2 commands fixed my case.这两个命令解决了我的情况。

"

I've had the same error message for a slightly different reason and was scratching my head for a long time over it.由于略有不同的原因,我收到了相同的错误消息,并且为它挠了很长时间。 Just posting here in case it helps anyone else.只是在这里发帖以防它对其他人有帮助。

vsts-npm-auth for me is outputting the new.npmrc file with its token/credentials into a.npmrc file in an old 'homes' share mapped to W: drive.对我来说,vsts-npm-auth 正在将 new.npmrc 文件及其令牌/凭据输出到映射到 W: 驱动器的旧“homes”共享中的 .npmrc 文件中。 (still don't know why it's doing that) (还是不知道为什么会这样)

whereas npm was looking for those credentials/tokens in c:\users\[username]\.npmrc and finding old stale creds.而 npm 正在 c:\users\[username]\.npmrc 中寻找那些凭证/令牌并找到旧的过时凭证。

my workaround was to copy and paste the creds from w:\.npmrc and paste them over the creds in c:\users\[username]\.npmrc我的解决方法是从 w:\.npmrc 复制并粘贴凭据并将它们粘贴到 c:\users\[username]\.npmrc 中的凭据上

We also encountered this issue when multiple scoped artifact feeds were in use but none of the answers here helped.当使用多个范围的工件提要时,我们也遇到了这个问题,但这里的答案都没有帮助。

Running vsts-npm-auth -config.npmrc worked successfully, but when running npm commands, it would respond with the error mentioned in the OP's question.运行vsts-npm-auth -config.npmrc成功,但是在运行 npm 命令时,它会响应 OP 问题中提到的错误。

We added the following line into our project's.npmrc file:我们在项目的 .npmrc 文件中添加了以下行:

always-auth=true

After doing this, we re-ran the npm command and successfully authenticated.做完这些,我们重新运行npm命令,认证成功。

First, make sure that you have installed 'vsts-npm-auth' package.首先,确保你已经安装了“vsts-npm-auth”包。 If not run:如果不运行:

npm install -g vsts-npm-auth

Then run vsts-npm-auth -config.npmrc然后运行vsts-npm-auth -config.npmrc

It will ask you to authenticate/verify your azure devops account.它会要求您验证/验证您的 azure devops 帐户。

Then run npm install然后运行npm install

follow the steps按照步骤

  1. npm install -g vsts-npm-auth
  2. vsts-npm-auth -config./$filepath/.npmrc you should mention the file path vsts-npm-auth -config./$filepath/.npmrc你应该提到文件路径

if the issue still exists then use(it will redirect to a popup there you want to re login with your credentials) 3. vsts-npm-auth -config./$filepath/.npmrc -r -f -v normal如果问题仍然存在,则使用(它将重定向到您要使用凭据重新登录的弹出窗口) 3. vsts-npm-auth -config./$filepath/.npmrc -r -f -v normal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM