Github的blog上传password authentication出错的解决方案

2021-08-20
#Blog

问题

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://github.com/xiaoledeng/xiaoledeng.github.io.git/': The requested URL returned error: 403

解决方式

解释:由于Github采用personal access token的方式,所以需要修改命令行的密码验证方式。

主要思想是:利用SSH key方式进行验证。

1. 生成SSH

  • 在终端输入:
$ ssh-keygen -t ed25519 -C "your_email@example.com" 

Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/name/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/name/.ssh/id_ed25519.
Your public key has been saved in /Users/name/.ssh/id_ed25519.pub.
The key fingerprint is:
SHA256:/QHOD/0oNoakMV2xSOTlOtdEh3INPsRttg your_email@example.com
The key's randomart image is:
+--[ED25519 256]--+
|        .=+=.+.  |
|       o*.o=*.   |
|      .o=oB.o    |
|       +.= B .   |
|    . . S O B    |
|     + . + & =   |
|      o   * E    |
|         . o .   |
|          . .    |
+----[SHA256]-----+

your_email@example.com 是 GitHub email address。

  • 输入密码
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

2. 把SSH加入到ssh-agent

  • 启动ssh-agent:
$ eval "$(ssh-agent -s)"
> Agent pid 59566
  • 生成文件:
$ touch ~/.ssh/config
  • 打开文件:
$ open ~/.ssh/config
  • 加入内容:
Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519
  • 储存keychain:
$ ssh-add -K ~/.ssh/id_ed25519

3. 把SSH key加入到GitHub账户

  • 复制SSH key
$ pbcopy < ~/.ssh/id_ed25519.pub
# Copies the contents of the id_ed25519.pub file to your clipboard

4. 把remote URLs从HTTPS改为SSH

  • 打开终端,切换到工作目录

  • 查看已有remote:

$ git remote -v
> origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
> origin  https://github.com/USERNAME/REPOSITORY.git (push)
  • 从HTTPS改为SSH:
$ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
  • 检验是否改了:
$ git remote -v
# Verify new remote URL
> origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
> origin  git@github.com:USERNAME/REPOSITORY.git (push)

5. Git上传到命令

# Add changes to git.
git init
git add -A

# Commit changes.
msg="building site `date`"
if [ $# -eq 1 ]
  then msg="$1"
fi
git commit -m "$msg"

# SSH
git remote add origin git@github.com:xiaoledeng/xiaoledeng.github.io.git
git branch -M main
git push origin main --force

参考