spr icon indicating copy to clipboard operation
spr copied to clipboard

use `gh` CLI to get OAuth token

Open ggoretkin-bdai opened this issue 2 years ago • 5 comments

There are a few ways that the gh CLI represents a token, including via an environment variable GH_TOKEN

In my development environment

gh auth status --show-token

displays the the token.

However:

$ git-spr --version
> git rev-parse --show-toplevel
4:48PM WRN ../../Users/runner/work/spr/spr/github/githubclient/client.go:89 > failed to read gh cli config file error="failed to open gh cli config file: open /home/ggoretkin/.config/gh/hosts.yml: no such file or directory"
4:48PM WRN ../../Users/runner/work/spr/spr/github/githubclient/client.go:101 > failed to read hub config file error="failed to open hub config file: open /home/ggoretkin/.config/hub: no such file or directory"

No GitHub OAuth token found! You can either create one
at https://github.com/settings/tokens and set the GITHUB_TOKEN environment variable,
or use the official "gh" CLI (https://cli.github.com) config to log in:

	$ gh auth login

Alternatively, configure a token manually in ~/.config/hub:

	github.com:
	- user: <your username>
	  oauth_token: <your token>
	  protocol: https

This configuration file is shared with GitHub's "hub" CLI (https://hub.github.com/),
so if you already use that, spr will automatically pick up your token.

ggoretkin-bdai avatar Jun 13 '23 16:06 ggoretkin-bdai

Note that as of https://github.com/cli/cli/releases/tag/v2.24.0 , the hosts.yml file is not used anymore.

ggoretkin-bdai avatar Jun 15 '23 16:06 ggoretkin-bdai

new user here, I'm confused by this as well, I did gh auth login however git spr status still says no token found

I've tried to configure manually token in ~/.config/hub by adding token from https://github.com/settings/tokens and gh auth status --show-token but both doesn't seem to work. how to configure this ?

update: storing the token in GH_TOKEN works

eko-wibowo avatar Jul 24 '23 20:07 eko-wibowo

I am trying to connect to our Github Enterprise server, and observe the same behaviour: Logged in fine when using gh auth status, but spr does not pick it up. I am unable to set up ~/.config/hub such that it would show any effect – perhaps the message here is not correct?

Setting the token in GITHUB_TOKEN as suggested in the help message works fine. (Not the one mentioned by @eko-wibowo.)

piefel avatar Dec 11 '23 09:12 piefel

TL;DR: gh auth login --insecure-storage let's you use gh as the source of the OAuth token.

For anyone struggling with this, spr tries to get the OAuth token from three sources:

  1. Environment variable GITHUB_TOKEN
  2. ~/.config/gh/hosts.yml
  3. ~/.config/hub

After gh release v2.26.0 logging in using gh auth login will no longer store the OAuth token as plain-text unless the --insecure-storage parameter is passed.

simenbrekken-visma avatar Feb 28 '24 07:02 simenbrekken-visma

Hello everyone,

First off, I'd like to say that this tool is fantastic—great work!

I have a question regarding the selection options for scopes. Specifically, how many options should I select to appropriately define the access level for personal tokens?

Thank you for your assistance!

carvierdotdev avatar May 18 '24 22:05 carvierdotdev