🔑 AddGitHubToken
Adds a GitHub OAuth token to Composer's auth.json, used to access private repositories or raise API rate limits.
When to use
Use when your project depends on private Composer repositories hosted on GitHub, or when you need to pull GitHub resources with an authenticated identity. The token is written to auth.json in the Composer home directory.
Signature
go
func (c *Composer) AddGitHubToken(domain string, token string) errorParameters
| Parameter | Type | Description |
|---|---|---|
domain | string | Domain the token applies to, typically github.com |
token | string | GitHub personal access token (PAT) |
Return value
error: Returns the corresponding error message when reading or writing auth.json fails; nil on success.
Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatalf("init failed: %v", err)
}
// Add a GitHub token
if err := comp.AddGitHubToken("github.com", "ghp_xxxxxxxxxxxxxxxxxxxx"); err != nil {
log.Fatalf("failed to add token: %v", err)
}
fmt.Println("GitHub token written to auth.json")
}Advanced
- Tokens are uniformly stored in the
AuthConfig.GitHubmap returned byGetAuthConfig, and persisted bySaveAuthConfig(file permission0600). - To remove a token, use
RemoveTokenwith typegithub-oauth. - To configure GitLab, Bearer, or HTTP Basic authentication, see
AddGitLabToken,AddBearerToken, andAddHTTPBasicAuthrespectively.