<p>Using go-git for a cloning a github repo. Trying to use personal token for authentication using something like this</p>

<pre><code>func (g *Git) pullOptions() *gogit.PullOptions {
branch := fmt.Sprintf("refs/heads/%s", g.BranchName)
// Return options with token auth if enabled
if g.GitToken != "" {
    log.Debug("Prepare pull option using gittoken")
    return &amp;gogit.PullOptions{
        ReferenceName: plumbing.ReferenceName(branch),
        Auth: &amp;githttp.BasicAuth{
            Username: g.GitUser,
            Password: g.GitToken,
        },
    }
}
</code></pre>

<p>Using <a href="https://github.com/davecgh/go-spew" rel="nofollow noreferrer">spew</a></p>

<p>can also see the pull options and they seem to be valid</p>

<pre><code>(*git.PullOptions)(0xc42008de60)({
RemoteName: (string) (len=6) "origin",
ReferenceName: (plumbing.ReferenceName) (len=17) refs/heads/master,
SingleBranch: (bool) false,
Depth: (int) 0,
Auth: (*http.BasicAuth)(0xc4203be300)(http-basic-auth - mygitid
:*******),
RecurseSubmodules: (git.SubmoduleRescursivity) 0,
Progress: (sideband.Progress) &lt;nil&gt;,
Force: (bool) false
})
</code></pre>

<p>But keep getting this error:</p>

<pre><code>time="2019-03-19T05:30:59Z" level=debug msg="Prepare pull option using 
gittoken"
time="2019-03-19T05:30:59Z" level=error msg="Git clone error: 
authentication required"
</code></pre>

<p>If I switch to SSHKey auth, then this works fine. Any pointers?</p>

<pre><code>EDIT-1:
</code></pre>

<p>It definitely seems to be an issue when pulling basic auth credentials from environment variables. For instance, this code does not work:</p>

<pre><code>package main

import (
    "fmt"
    "os"
    "time"

    log "github.com/Sirupsen/logrus"
    gogit "gopkg.in/src-d/go-git.v4"
    gitconfig "gopkg.in/src-d/go-git.v4/config"
    "gopkg.in/src-d/go-git.v4/plumbing"
    "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)

func main() {
    var repository *gogit.Repository
    var err error

    // @TODO: Why not use clone?
    if _, err = os.Stat("/tmp/repo"); os.IsNotExist(err) {
        repository, err = gogit.PlainInit("/tmp/repo", false)
        if err != nil {
            log.Errorf("could not init local repository %s: %s", "/tmp", err.Error())
        }
    } else {
        repository, err = gogit.PlainOpen("/tmp/repo")
    }

    //fmt.Println((repository))
    if _, err = repository.Remote("origin"); err == gogit.ErrRemoteNotFound {
        _, err = repository.CreateRemote(&amp;gitconfig.RemoteConfig{
            Name: "origin",
            URLs: []string{"https://xxxxx.git"},
        })
        if err != nil {
            log.Errorf("could not attach to origin %s: %s", "bb", err.Error())
        }
    }

    fmt.Println("Done with mapping")


    r, err := gogit.PlainOpen("/tmp/repo")
    if err != nil {
        log.Fatal(err)
    }
    //fmt.Println(r)
    branch := fmt.Sprintf("refs/heads/%s", "master")

    fmt.Println("Setup wotktree")
    w, err := r.Worktree()
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("pulling")
    fmt.Println(os.Getenv("GIT_USER"))
    fmt.Println(os.Getenv("GIT_TOKEN"))
    if err := w.Pull(&amp;gogit.PullOptions{
        ReferenceName: plumbing.ReferenceName(branch),
        Auth: &amp;http.BasicAuth{
            // Username: "xxxxxx",
            // Password: "xxxxxxxxxx",
            Username: os.Getenv("GIT_USER"),
            Password: os.Getenv("GIT_TOKEN"),
        },
    }); err != nil {
        log.Fatal(err)
    }
    fmt.Println("done")
    time.Sleep(120 * time.Second)
}
</code></pre>

<p>However, If I hardcode credentials as below, then it works. </p>

<pre><code>    Auth: &amp;http.BasicAuth{
        Username: "xxxxxx",
        Password: "xxxxxxxxxx",
        // Username: os.Getenv("GIT_USER"),
        // Password: os.Getenv("GIT_TOKEN"),
    },
</code></pre>

<p>So the question now is really, how do we securely pass credentials to go-git for basic auth?  do we carve a credential helper for git but then the point of go-git not relying on native client may be defeated. </p>

f-z-N
<p>A working example for people who would get into troubles :</p>

<pre><code>package main

import (
    "fmt"
    "log"

    "gopkg.in/src-d/go-git.v4"
    "gopkg.in/src-d/go-git.v4/plumbing"
    "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)

func main() {
    r, err := git.PlainOpen("&lt;REPOSITORY_PATH&gt;")
    if err != nil {
        log.Fatal(err)
    }

    branch := fmt.Sprintf("refs/heads/%s", "master")

    w, err := r.Worktree()
    if err != nil {
        log.Fatal(err)
    }

    if err := w.Pull(&amp;git.PullOptions{
        ReferenceName: plumbing.ReferenceName(branch),
        Auth: &amp;http.BasicAuth{
            Username: "&lt;GITHUB_USERNAME&gt;",
            Password: "&lt;GITHUB_API_KEY&gt;",
        },
    }); err != nil {
        log.Fatal(err)
    }
}
</code></pre>
