19

当我尝试npm从 GitHub 操作安装我的模块时,我收到以下错误:

npm ERR! 401 Unauthorized - GET https://npm.pkg.github.com/@xxxx%2fxxxx-analytics - Your request could not be authenticated by the GitHub Packages service. Please ensure your access token is valid and has the appropriate scopes configured.

在您发表评论之前,我已经使用范围和访问令牌正确配置了 .npmrc,并且在本地安装私有包时一切正常。

这是我的 GitHub 工作流程操作:

name: JavaScript workflow

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1
      - name: Use Node.js 12.x
        uses: actions/setup-node@v1
        with:
          node-version: '12.x'
      - name: npmrc
        run: cat .npmrc
      - name: npm install
        run: |
          npm install
        env:
          CI: true
          NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

这是我的 .npmrc

@fortawesome:registry=https://npm.fontawesome.com/
//npm.fontawesome.com/:_authToken=XXXXXXXXX
@colonynetworks:registry=https://npm.pkg.github.com
//npm.pkg.github.com:_authToken=XXXXXXXXX
always-auth=true
@react-admin:registry=https://registry.marmelab.com
//registry.marmelab.com:
_auth=XXXXXXXXX
email=software@XXXXXXXXX.com
always-auth=true

这是一个私人仓库,authTokens 当前被硬编码在 .npmrc 文件中。

然而,在尝试为此寻找解决方案时,我确实遇到了 Github 工作人员的这条随机评论:https ://github.community/t/netlify-getting-401-from-github-package-registry-with-auth -令牌/16415/3

这有点含糊,但听起来它不接受 .npmrc 文件中的硬编码 authToken 。

所以我尝试的第一件事是使用我们的环境变量,而不是像这样:

@xxxx=https://npm.pkg.github.com
//npm.pkg.github.com:_authToken=${NPM_AUTH_TOKEN}

env 变量在我们的 Github 存储库机密中是正确的,并由工作流提供。

但是,这仍然导致相同的 401 Unauthorized 错误。

通过查看其他解决方案,我尝试在install步骤之前在 Github 操作中手动生成 .npmrc,如下所示:

- name: npmrcgen
        run: |
          echo "//npm.pkg.github.com/:_authToken=XXXXXXX" > .npmrc
          echo "@xxxxx=https://npm.pkg.github.com/" >> .npmrc
          echo "@react-admin:registry=https://registry.marmelab.com" >> .npmrc
          echo "//registry.marmelab.com:" >> .npmrc
          echo "_auth=XXXXXXX" >> .npmrc
          echo "email=software@xxxxx.com" >> .npmrc
          echo "always-auth=true" >> .npmrc

在我添加的日志记录步骤中,_authToken(仅适用于 Github)仍然显示为***,并且我仍然收到 401 Unauthorized 错误。

在这一点上,我想确认 .npmrc 甚至被使用,所以我删除了我们用于的第二个私有注册表marmelab.com,果然,我收到一个错误,说它不再能够安装他们的ra-realtime包。这证明 .npmrc 文件确实被我的 Github 操作读取和使用,但它不接受我的 Github 个人访问令牌。

我也尝试生成一个新令牌。它可以完全访问下面的所有内容repo:以及应该需要的内容write:packagesread:packages

在 Github 操作中仍然 401 Unauthorized,并且在本地仍然可以正常工作。

最后,我尝试使用yarn而不是npm. 不出所料,这也没有解决它。

我已经看到并尝试了以下解决方案,但没有成功:

一件事我没有尝试过,因为我没有看到关于如何或这是一个好主意的建议,但我还没有npm login在 Github 内进行操作。由于没有其他人这样做,并且以某种方式使其工作,我认为这没有必要。

4

2 回答 2

16

我最终不得不联系 GitHub 支持并让他们访问我的存储库来解决这个问题。

然而,他们确实弄清楚了问题所在。

/Github 工作流程比本地环境更严格,并且在 auth 令牌之前需要额外的:

指出不同:

//npm.pkg.github.com:_authToken=XXXXXXXXX
//npm.pkg.github.com/:_authToken=XXXXXXXXX

在为我解决问题/之前添加额外的内容。:_authToken=

于 2020-10-13T16:39:12.900 回答
5

在项目的根目录中有一个 .npmrc 文件。

.npmrc 的内容:

registry=https://registry.npmjs.org/
@{scope}:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=********** (Token generated from github)

@{scope} 是您的组织名称或用户名。它区分大小写。

于 2021-05-21T19:36:30.547 回答