【问题标题】:Set github personal access token in visual studio code在 Visual Studio 代码中设置 github 个人访问令牌
【发布时间】:2021-10-17 12:00:59
【问题描述】:

我正在尝试在 VS 代码中设置个人访问令牌,但它似乎不接受它。我把它放在命令面板的“Github:设置个人访问令牌”中,但它似乎没有任何效果。我还将令牌放在 Windows 凭据管理器中。

当我尝试推送时,我收到此错误:

remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: unable to access 'https://<usr>:token@github.com/<us>/repository.git/': The requested URL returned error: 403

【问题讨论】:

标签: git github visual-studio-code


【解决方案1】:

确保您拥有适用于 Windows 的最新 Git,并将 git config --global credential.helper 设置为 manager-core

从那里,使用CMD (not git bash) 检查您是否确实将令牌作为密码存储在凭据帮助程序中:

printf "protocol=https\nhost=github.com"|"C:\Program Files\Git\mingw64\libexec\git-core\git-credential-manager-core.exe" get
protocol=https
host=github.com
username=VonC
password=ghp_Dxc...<yourToken>

确保令牌以ghp_ 开头(在new token format update 之后)。

如果没有,删除它:

printf "protocol=https\nhost=github.com"|"C:\Program Files\Git\mingw64\libexec\git-core\git-credential-manager-core.exe" earase

然后下一次推送将询问并存储您的新凭据。
无需手动调整 Windows 凭据管理器:这就是这些助手的用途。

来自the discussion,问题是双重的:

  • 系统用户 Windows 路径未引用 Git 安装路径
  • 在本地存储库中注册的远程 URL 确实直接包含令牌(而不是依赖 git 凭据管理器)

解决这两个问题允许 VSCode 开始使用正确的凭据进行推送。

【讨论】:

  • 我重新安装了新版本的 git,将 credential.helper 设置为 manager-core,那里似乎没有那个文件:C:\Users/bfers/git/gitcpl> printf "protocol=https/nhost=github.com"|"C:\Program Files/Git/mingw64/libexec/git-core/git-credential-manager-core.exe" 得到 bash: C:Users/bfers/git /gitcpl:没有这样的文件或目录致命:缺少“协议”输入参数。我的 git 文件是 .git,当我添加点时它失败了
  • @BillFerster 抱歉,C:\Users\&lt;aUsername&gt;\git\gitcpl 只是我输入printf/git-credential-manager-core.exe 命令时所在的文件夹。您可以在任何您想要的文件夹中执行它。但是在 CMD 中。不在 git bash 中。
  • 我的 toke 确实是 "ghp..." 很抱歉很密集,但是我该如何将该文件写入我的桌面?
  • @BillFerster 确保您已删除旧的 github.com 条目(如果 git-credential-manager-core.exe get 向您显示旧密码。然后下一个 git push 将提示您输入新凭据并缓存它们。使用您的 GitHub帐户名和令牌。
  • @BillFerster VSCode 将“知道”令牌,因为它将使用与 CLI 中相同的凭据帮助器:manager-core。
【解决方案2】:

如果您使用的是 Windows 10,请搜索“管理网络密码” - 它会带您在控制面板中打开凭据管理器。在列表中找到git:https://github.com(不是GitHub - https://api.github.com/username)并将密码更改为您的个人访问令牌create on the GitHub site

完成此操作后,我能够使用 VS Code 中的 GitHub 集成正常推送。

【讨论】:

  • 只有 git://bferster@github.com,所以我添加了 git:github.com,在两者中都设置了令牌,但推送时仍然得到相同的错误消息
  • @BillFerster 否:如我的回答所述,您需要删除这些条目。
  • 我现在完全糊涂了。我删除了两个条目,但仍然没有喜悦。 vscode怎么会知道token
【解决方案3】:

克隆的仓库太多了吗?

全局存储令牌将很容易使用。

如果你的机器上有很多克隆的存储库,我认为最好的方法是将 Github 令牌全局存储在 git credential.helper 中。

假设您手头有一个令牌,并且正在尝试运行任何需要身份验证的 git 命令,即使在 VSCode 中,步骤如下:

  1. 输入您的用户名;
  2. 在密码字段输入之前生成的token作为密码;
  3. 运行命令将令牌保留在 credential.helper 缓存中;

第 3 步的命令:

git config --global credential.helper cache

从本地计算机清除令牌:

git config --global --unset credential.helper

凭证缓存存在与令牌存储时间相关的问题。要控制这个时间,有必要设置“超时”参数或更改策略以通过将“缓存”更改为“存储”来永久存储凭据,如下所示:

步骤 3 的补充命令(以秒为单位的超时):

# timeout default is 900 seconds
git config --global --set credential.helper 'cache --timeout=3600'

第 3 步的命令(永久存储):

git config --global credential.helper store

使用此帮助程序会将未加密的密码/令牌存储在磁盘上,仅受文件系统权限保护。

有用的参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-19
    • 2021-12-12
    • 2021-09-04
    • 1970-01-01
    • 2021-12-26
    • 2021-09-30
    • 2021-10-19
    • 2019-12-18
    相关资源
    最近更新 更多