GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix GitHub Copilot authentication flow by bitomule · Pull Request #294 · opencode-ai/opencode

This repository was archived by the owner on Sep 18, 2025. It is now read-only.

Conversation

@bitomule

@bitomule bitomule commented Jul 4, 2025

Copy link
Copy Markdown

Summary

This PR fixes the GitHub Copilot authentication flow in OpenCode, which currently has issues with token retrieval and exchange.

Problem Description

Currently, the GitHub Copilot integration has several issues:

  1. The code tries to use standard GitHub tokens (like those from gh auth token) which don't work with Copilot API because they lack the required Copilot scope
  2. There's no proper authentication flow when no token is found
  3. The token exchange process fails due to missing required headers and incorrect API version
  4. Missing documentation on how to properly set up and use the Copilot integration

Solution

This PR implements a complete authentication flow inspired by the one used in the Neovim Copilot plugin:

  1. Properly checks for Copilot tokens in standard locations (hosts.json and apps.json)
  2. Implements OAuth device code flow when no token is found
  3. Adds proper GitHub API headers including the correct API version (X-GitHub-Api-Version: 2022-11-28)
  4. Uses the standard Copilot client ID (same as used by VS Code and Neovim plugins)
  5. Stores tokens in the standard location for reuse across sessions

Documentation

Added comprehensive documentation to the README explaining:

  • How to configure Copilot in OpenCode
  • The authentication flow process
  • Where tokens are stored

This PR makes Copilot integration work seamlessly with OpenCode, especially important for users wanting to access Claude models through Copilot.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mendesbarreto and norandom reacted with rocket emoji
All reactions
  • 🚀 2 reactions
David Collado Sela added 7 commits July 4, 2025 15:34
David Collado Sela David Collado Sela
Improve GitHub Copilot authentication flow
af3de60
Implement a simplified 4-step authentication flow:
1. Check if Copilot is enabled in config
2. Check for token in config folder
3. If no token, trigger login flow
4. With token ready, open OpenCode normally

Key improvements:
- Cleaner code structure with helper functions
- Better error handling for auth scenarios
- Support for both hosts.json and apps.json formats
- Proper API version headers for GitHub Copilot
- Clear documentation of client ID usage
David Collado Sela David Collado Sela
Add GitHub Copilot authentication documentation to README
4e625d8
David Collado Sela David Collado Sela
Simplify Copilot provider validation logic
2855bd8
- Remove redundant checks for Copilot provider in Validate()
- Improve comments for better clarity
- Maintain the same functionality with cleaner code
David Collado Sela David Collado Sela
Clean up excessive logging
0055afc
- Simplify logging in GitHub Copilot authentication
- Remove redundant logging messages
- Remove unused getMapKeys helper function
- Keep only essential user feedback during authentication
David Collado Sela David Collado Sela
Improve GitHub Copilot authentication flow and clean implementation
aaa274f
- Implement device flow authentication for GitHub Copilot
- Add token saving to standard ~/.config/github-copilot/hosts.json
- Fix token validation and error handling
- Continue polling when receiving empty responses
- Set environment variables for immediate use of token
- Clean and simplify code implementation
David Collado Sela David Collado Sela
Clean and simplify Copilot token handling code
71ae814
- Remove redundant token saving functions
- Combine hosts.json handling in a single function
- Simplify environment variable checks
- Remove excessive debug logging
- Streamline error handling and code paths
David Collado Sela David Collado Sela
Remove unnecessary configuration validation log message
1f64352
mendesbarreto
mendesbarreto reviewed Jul 5, 2025
View reviewed changes
Comment thread internal/llm/provider/copilot.go Outdated
}

// Set environment variables for immediate use
os.Setenv("GITHUB_TOKEN", token)

@mendesbarreto mendesbarreto Jul 5, 2025

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid conflicts with other tools like the GitHub CLI (gh) that uses GITHUB_TOKEN, I think we should use only GITHUB_COPILOT_TOKEN for all Copilot-related requests using opencode and leave GITHUB_TOKEN untouched.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seanf and bitomule reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
Comment thread internal/config/config.go Outdated
logging.Debug("Attempting to load GitHub token for Copilot")

// First check environment variables
for _, envName := range []string{"GITHUB_TOKEN", "GITHUB_COPILOT_TOKEN", "GH_COPILOT_TOKEN"} {

@mendesbarreto mendesbarreto Jul 5, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, I was just about to create a PR for this myself. We should move away from GITHUB_TOKEN for the reason mentioned here. All the other tools like Gemini and ChatGPT use their own environment variables, so it makes sense to give Copilot its own too. Using GITHUB_TOKEN can be misleading as copilot request don't accept them.

For me this method should be something like this:

func LoadGitHubToken() (string, error) {
	// 1. Environment variable
	var token string
	if token = os.Getenv("GITHUB_COPILOT_TOKEN"); token != "" {
		logging.Debug("Loaded GitHub Copilot API key from a system environment variable.")
		return token, nil
	}

	// 2. API key from options
	if token = cfg.Providers[models.ProviderCopilot].APIKey; token != "" {
		logging.Debug("Loaded GitHub Copilot API key from the '.opencode.json' configuration file.")
		return token, nil
	}

	// Get config directory
	var configDir string
	if xdgConfig := os.Getenv("XDG_CONFIG_HOME"); xdgConfig != "" {
		configDir = xdgConfig
	} else if runtime.GOOS == "windows" {
		if localAppData := os.Getenv("LOCALAPPDATA"); localAppData != "" {
			configDir = localAppData
		} else {
			configDir = filepath.Join(os.Getenv("HOME"), "AppData", "Local")
		}
	} else {
		configDir = filepath.Join(os.Getenv("HOME"), ".config")
	}

	// Try both hosts.json and apps.json files
	filePaths := []string{
		filepath.Join(configDir, "github-copilot", "hosts.json"),
		filepath.Join(configDir, "github-copilot", "apps.json"),
	}

	for _, filePath := range filePaths {
		data, err := os.ReadFile(filePath)
		if err != nil {
			continue
		}

		var config map[string]map[string]interface{}
		if err := json.Unmarshal(data, &config); err != nil {
			continue
		}

		for key, value := range config {
			if strings.Contains(key, "github.com") {
				if oauthToken, ok := value["oauth_token"].(string); ok {
					logging.Debug("Loaded GitHub Copilot token from the standard user configuration file.")
					return oauthToken, nil
				}
			}
		}
	}

	return "", fmt.Errorf("GitHub token not found in standard locations")
}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mendesbarreto

mendesbarreto commented Jul 5, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

I tested your PR but ran into this error:

image

This happens because my GITHUB_TOKEN is being rejected by Copilot. This creates a frustrating workflow for anyone who relies on that token for other tools. We shouldn't have to unset an important variable just to open opencode.

So Like I said before we need to move away from GITHUB_TOKEN and use the token from $XDG_CONFIG_HOME/opencode or $XDG_CONFIG_HOME/github-copilot.

If we do this we can fix #294

Thank you so much for the effort =)

bitomule reacted with thumbs up emoji bitomule and squanto reacted with heart emoji
All reactions
  • 👍 1 reaction
  • ❤️ 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mendesbarreto mendesbarreto mentioned this pull request Jul 5, 2025
Error: Failed to process events: request config: base url is not set #270
Open
David Collado Sela opencode David Collado Sela
Fix GitHub Copilot authentication conflicts by prioritizing GITHUB_CO…
5abea93
…PILOT_TOKEN

Address the issue where GITHUB_TOKEN conflicts with GitHub CLI tools by:
- Prioritizing GITHUB_COPILOT_TOKEN environment variable over GITHUB_TOKEN
- Updating token loading logic to avoid CLI token conflicts
- Modifying error messages to reference the correct token variable
- Updating documentation to reflect the new token preference

This resolves 401 Unauthorized errors when users have standard GitHub tokens without Copilot scope.

🤖 Generated with opencode
Co-Authored-By: opencode <[email protected]>
@bitomule

bitomule commented Jul 7, 2025

Copy link
Copy Markdown
Author

Hey @mendesbarreto pushed a new commit with your feedback. Is this what you had in mind? Thank you for your comments!

mendesbarreto reacted with heart emoji mendesbarreto reacted with rocket emoji
All reactions
  • ❤️ 1 reaction
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mendesbarreto

mendesbarreto commented Jul 8, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

It's working =)

image
image

fixes #270

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bitomule

bitomule commented Jul 9, 2025

Copy link
Copy Markdown
Author

What's the next step to get it merged?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mendesbarreto

mendesbarreto commented Jul 10, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

What's the next step to get it merged?

I have been following the tale of Copilot since its conception, and the last one who approved something similar was @kujtimiihoxha. 😊

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mendesbarreto

mendesbarreto commented Jul 11, 2025

Copy link
Copy Markdown

@bitomule I think the why is here:

  • Refactor in progress #228
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kujtimiihoxha

kujtimiihoxha commented Jul 12, 2025

Copy link
Copy Markdown
Contributor

Hey guys, really sorry for the slow reply here. I am in the middle of a huge refactor and rebrand for opencode, would love it if you guys submit the early access form so you get to try the new version earlier https://charm.sh/early-access

squanto, mendesbarreto, and Olyxz16 reacted with rocket emoji
All reactions
  • 🚀 3 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mendesbarreto

mendesbarreto commented Jul 14, 2025

Copy link
Copy Markdown

Hey guys, really sorry for the slow reply here. I am in the middle of a huge refactor and rebrand for opencode, would love it if you guys submit the early access form so you get to try the new version earlier https://charm.sh/early-access

subscribed ✅ !!!!!!!!!!!

Thank you so much @kujtimiihoxha

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@wubin1989 wubin1989 mentioned this pull request Apr 27, 2026
Fix GitHub Copilot authentication flow wubin1989/opencode#27
Open
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

1 more reviewer
@mendesbarreto mendesbarreto mendesbarreto left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@bitomule @mendesbarreto @kujtimiihoxha
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.