gopher-and-monalisa

個人開発していて、GitHubの自分のプライベートリポジトリをgo getしようとして、エラーに遭遇しました。 今後同じような目にあった時のために、備忘録として残します。

エラー内容

以下のようなエラーが発生しました。

$ go get github.com/<username>/<private-repository-name>

go: downloading github.com/<username>/<private-repository-name> v0.0.0-20200426100620-259de4f4dd2c
go get github.com/<username>/<private-repository-name>: github.com/<username>/<private-repository-name>@v0.0.0-20200426100620-259de4f4dd2c: verifying module: github.com/<username>/<private-repository-name>@v0.0.0-20200426100620-259de4f4dd2c: reading https://sum.golang.org/lookup/github.com/<username>/<private-repository-name>@v0.0.0-20200426100620-259de4f4dd2c: 410 Gone
	server response:
	not found: github.com/<username>/<private-repository-name>@v0.0.0-20200426100620-259de4f4dd2c: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /tmp/gopath/pkg/mod/cache/vcs/d99582f38bf8799347c5c3580859733d5d5c417fd23feb23ec82dc0787605157: exit status 128:
		fatal: could not read Username for 'https://github.com': terminal prompts disabled

原因

go getでモジュールをダウンロードする際は通常、http通信をしているみたいです。 GitHubのプライベートリポジトリへは認証が通らず、エラーになっていました。

解決方法

以下手順で解決できます。

  1. Personal access tokensを作成
  2. git config設定

1. Personal access tokensを作成

GitHubのDeveloper settings画面でトークンを作成できます。 設定画面を開いたら、「Generate new token」を押すと、以下の画面が表示されます。

設定画面
  • Noteに何用のトークンか記載
  • repoにチェックをいれる

これでトークンを作成できました。

2. git config設定

以下コマンドを実行します。

$ git config --global url."https://<  your token  >:x-oauth-basic@github.com/".insteadOf "https://github.com/"

実行をすると、~/.gitconfigに追記されて、go getをすることができるようになります。