pygit2 clone_repository authentication required but no callback set

2.8k views Asked by At

I'm trying to clone a repository from stash using an ssh link. I get an error saying authentication is required, This should not require a username and password. How do I fix this error?

from pygit2 import *
repo_name = "ssh://git@stash:TheProject"
clone_repository(self.repo_name, self.repo_dir, credentials=cred)

Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__ return self.func(*args)
File "rio_telem_tool.py", line 143, in build clone_repository(self.repo_name, self.repo_dir)
File "/usr/local/lib/python2.7/dist-packages/pygit2/__init__.py", line 265, in clone_repository check_error(err)
File "/usr/local/lib/python2.7/dist-packages/pygit2/errors.py", line 56, in check_error raise GitError(message)
GitError: authentication required but no callback set
1

There are 1 answers

0
Eric Van Bezooijen On

This is an old question, but I recently had two issues to solve:

  1. How to give pygit2 the username and git token to connect to github
  2. Since this was a completely internal application, how to skip the certificate check that was blocking the connection to github

The solution to both involved subclassing pygit2.RemoteCallbacks and calling it like this:

 pygit2.clone_repository(your_args, callbacks=self.MyRemoteCallBacks(user,token)

The class:

class MyRemoteCallbacks(pygit2.RemoteCallbacks):
        def __init__(self, user, token):
            self.user = user
            self.token = token
        def credentials(self, url, username_from_url, allowed_types):
            return pygit2.UserPass(self.user,self.token)
        def certificate_check(self, certificate, valid, host):
            return True