google / go-github

Go library for accessing the GitHub v3 API
https://pkg.go.dev/github.com/google/go-github/v67/github
BSD 3-Clause "New" or "Revised" License
10.47k stars 2.07k forks source link

Invalid header error message when authenticating to GHE #2675

Open jm252409 opened 1 year ago

jm252409 commented 1 year ago

I've been trying to use this to authenticate to GHE. Having some issues:

2023/02/24 18:19:20 error: Get "https://github.ibm.com/api/v3/user/repos": net/http: invalid header field value for "Authorization"

Here's my code, I validated that my Personal Access token is coming in correctly and everything. I used the "NewEnterpriseClient" and followed the instructions reguarding my base URL/upload URL.

I've tripple checked my stuff and don't see anything wrong. Does this client work with GHE? Can anyone else verify?

package main

import (
    "bytes"
    "context"
    "fmt"
    "github.com/google/go-github/v50/github"
    "golang.org/x/oauth2"
    "log"
    "os/exec"
)

const ShellToUse = "bash"

func Shellout(command string) (string, string, error) {
    var stdout bytes.Buffer
    var stderr bytes.Buffer
    cmd := exec.Command(ShellToUse, "-c", command)
    cmd.Stdout = &stdout
    cmd.Stderr = &stderr
    err := cmd.Run()
    return stdout.String(), stderr.String(), err
}
func main() {

    token, errout, err := Shellout("echo $(pass github-keys/jimmy-pat)")
    if err != nil {
        log.Printf("error: %v\n", errout)
    }

    httpClient := oauth2.NewClient(
        context.Background(),
        oauth2.StaticTokenSource(
            &oauth2.Token{AccessToken: token},
        ))

    client, err := github.NewEnterpriseClient(
        "https://github.ibm.com/api/v3/",
        "https://github.ibm.com/api/v3/uploads/",
        httpClient,
    )
    if err != nil {
        log.Printf("error: %v\n", err)
    }

    // list all organizations for user "willnorris"
    orgs, response, err := client.Repositories.List(
        context.Background(),
        "",
        nil,
    )

    println("the response: ", response)
    if err != nil {
        log.Printf("error: %v\n", err)
    }
    for _, org := range orgs {
        fmt.Printf("the org: %v", org)
    }

}
gmlewis commented 1 year ago

Just for debugging purposes, try to simplify your local test code (do NOT share it online) by temporarily hard-coding your personal access token as a string, because it sounds like "net/http" is saying that it contains garbage (possibly a non-ASCII string, just a guess).

Your personal access token should look something like this: "abc_123...xyz" and somewhere around 40 characters long, made up of upper case letters, lower case letters, digits, and underscores.

Another debugging thing to try is to use something like https://github.com/gmlewis/go-httpdebug to dump a curl-equivalent version of your command... and as a last resort, modify "go-httpdebug" to actually print your token instead of hiding it, just to verify it is getting through.

Otherwise, I don't see any problems. Good luck, I hope you find the problem soon.