Skip to content

gh api suggests auth refresh for missing scopes on an environment token #15129

Open

Description

@konard

Describe the bug

When an environment-provided classic token lacks a scope, gh api suggests gh auth refresh. The suggested command fails because gh cannot update an environment-provided token. I reproduced this with gh 2.105.0 and 2.102.0 on Linux using an isolated loopback API and a synthetic token.

Steps to reproduce

  1. In an isolated shell, export a classic personal access token that has repo and lacks both user and user:email as GH_TOKEN.
  2. Run gh api --hostname github.com user/emails.
  3. Run the command printed in the scope hint.

The same diagnostic path can be reproduced without a real token or account. Configure GH_HOST=127.0.0.1 and a dummy GH_ENTERPRISE_TOKEN with a temporary GH_CONFIG_DIR. Serve HTTPS locally for GET /api/v3/user/emails and return HTTP 404 with {"message":"Not Found"}, Content-Type: application/json, X-OAuth-Scopes: repo, and X-Accepted-OAuth-Scopes: user, user:email. Trust the local certificate for the experiment and run the two commands below.

$ gh api --hostname 127.0.0.1 user/emails
{"message":"Not Found"}
gh: Not Found (HTTP 404)
gh: This API operation needs the "user" scope. To request it, run:  gh auth refresh -h 127.0.0.1 -s user

$ gh auth refresh -h 127.0.0.1 -s user
The value of the GH_ENTERPRISE_TOKEN environment variable is being used for authentication.
To refresh credentials stored in GitHub CLI, first clear the value from the environment.

Both commands exit 1. The email API documentation describes user:email as sufficient for classic tokens.

Expected behavior and proposed change

Keep the missing-scope information and tailor the next step to the active credential. For an environment token, name the environment variable and suggest replacing its value with a credential that has the required permission. Suggest gh auth refresh for eligible stored credentials.

In the API command's error handling, resolve the token source for the request host using the same authentication configuration as the API transport. Pass the source into the scope hint formatter, or pass back the missing scope and choose the remediation at the call site. The refresh command already distinguishes environment-provided tokens. Test the same 404 response with a stored token and an environment-provided token and assert the appropriate hint for each.

Workaround

Replace the exported token with a classic token that has user:email or a fine-grained user token with Email addresses: Read. To use stored credentials instead, clear the applicable environment token and sign in with gh auth login -h github.com -s user:email --web.

This is a concrete example of credential-specific remediation under the actionable errors design. It was investigated while working on gh-setup-git-identity issue 34.

Runnable loopback reproduction

Activity

  1. cli-triage commented on Nov 22, 2026

    @cli-triage

    Partly reproduced on 2.105.0.

    Using test data with a token that has repo but not user, the API command printed the same refresh hint:

    $ gh api --hostname github.com user/emails
    {"message":"Not Found"}
    gh: Not Found (HTTP 404)
    gh: This API operation needs the "user" scope. To request it, run:  gh auth refresh -h github.com -s user

    Exit status: 1.

    The same result appears on release 2.105.0 and source de06af2. The test setup could not preserve an environment-token source for the follow-up command, but current source confirms gh auth refresh rejects environment tokens. The hint can therefore direct users to a command that cannot update their active credential.

    Supporting reports

    Generated by issue triage.

  2. added
    bug
    gh-api
    priority-3
    on Nov 22, 2026
  3. removed
    needs-triage
    on Nov 22, 2026
  4. army20152015-design commented on Nov 22, 2026

    @army20152015-design
  5. Shanmukh-l33t commented on Nov 23, 2026

    @Shanmukh-l33t
  6. added
    auto-bugfix-ineligible
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    auto-bugfix-ineligiblebuggh-apipriority-3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions