pygit2 icon indicating copy to clipboard operation
pygit2 copied to clipboard

Pygit2 cannot parse URLs with OAuth tokens.

Open ghost opened this issue 9 years ago • 8 comments

I'm trying to use pygit2 to clone a GitHub repo using OAuth tokens. This actually works well from the command line, as explained on GitHub's blog here.

So, I do:

from pygit2 import clone_repository

oauth_token = 'somesuperlongtokengivenbygithub'
repo = clone_repository(
                'https://'+oauth_token+'@github.com/openwrt/luci', 
                '/tmp/repos', 
                bare=True
)

and I get back:

00:04:42 _pygit2.GitError: Unsupported URL protocol
Traceback (most recent call last):
  File "/home/ubuntu/.venv/etincelle/lib/python3.4/site-packages/rq/worker.py", line 558, in perform_job
    rv = job.perform()
  File "/home/ubuntu/.venv/etincelle/lib/python3.4/site-packages/rq/job.py", line 495, in perform
    self._result = self.func(*self.args, **self.kwargs)
  File "/home/ubuntu/repo/etincelle/routes.py", line 41, in clone_all_repos
    clone(repo, _app, token)
  File "/home/ubuntu/repo/etincelle/routes.py", line 25, in clone
    clone_repository(oauth_url, git_id_repo, bare=True)
  File "/home/ubuntu/.venv/etincelle/lib/python3.4/site-packages/pygit2/__init__.py", line 293, in clone_repository
    check_error(err)
  File "/home/ubuntu/.venv/etincelle/lib/python3.4/site-packages/pygit2/errors.py", line 64, in check_error
    raise GitError(message)
_pygit2.GitError: Unsupported URL protocol
Traceback (most recent call last):
  File "/home/ubuntu/.venv/etincelle/lib/python3.4/site-packages/rq/worker.py", line 558, in perform_job
    rv = job.perform()
  File "/home/ubuntu/.venv/etincelle/lib/python3.4/site-packages/rq/job.py", line 495, in perform
    self._result = self.func(*self.args, **self.kwargs)
  File "/home/ubuntu/repo/etincelle/routes.py", line 41, in clone_all_repos
    clone(repo, _app, token)
  File "/home/ubuntu/repo/etincelle/routes.py", line 25, in clone
    clone_repository(oauth_url, git_id_repo, bare=True)
  File "/home/ubuntu/.venv/etincelle/lib/python3.4/site-packages/pygit2/__init__.py", line 293, in clone_repository
    check_error(err)
  File "/home/ubuntu/.venv/etincelle/lib/python3.4/site-packages/pygit2/errors.py", line 64, in check_error
    raise GitError(message)
_pygit2.GitError: Unsupported URL protocol

ghost avatar Aug 23 '15 00:08 ghost

What's the format of the OAuth token? If it's ASCII there shouldn't be any issues, as it's just a username, but the error message suggests that it's the prefix which it doesn't recognise, which could simply be that no supported crypto library was found during libgit2's build.

Does a clone succeed if you don't put the token in the URL? It's unnecessary in this case.

carlosmn avatar Sep 12 '15 02:09 carlosmn

Here's what an 'access_token' looks like: e72e16c7e42f292c6912e7710c838347ae178b4a It's basically just a string of [a-z0-9] characters. More info on the the token can be found here: https://developer.github.com/v3/oauth/

There's a very good reason why you would want to do that as explained in the link I put in the initial description of this ticket. The clone will not succeed if you don't provide a token, no. The goal is to clone private repos, as obviously you don't need OAuth stuff to clone public repos. My example used a public repo simply because it's easier to test than pointing to a private repo you have no access to.

ghost avatar Sep 20 '15 00:09 ghost

The clone will not succeed if you don't provide a token, no. The goal is to clone private repos, as obviously you don't need OAuth stuff to clone public repos. My example used a public repo simply because it's easier to test than pointing to a private repo you have no access to.

I get that you want to use a token to clone, but that doesn't answer the question. Is a clone attempted when you don't provide the token in the URL? The error message would be generated without looking at that part of the string, so it's rather likely that the token is not related to this situation. And it would help if you could confirm whether not having the token makes the URL parseable.

The most likely scenario here is that either your copy of libgit2 was not build with HTTPS support, or that the string you pass to pygit2 is mangled in some way which makes "http://" not be the initial part of the string.

carlosmn avatar Sep 20 '15 04:09 carlosmn

@bokehsensei I was able to get clone to work on a private Github repo using OAuth tokens:

import pygit2
callbacks = pygit2.RemoteCallbacks(pygit2.UserPass('mytoken', 'x-oauth-basic'))
repo = pygit2.clone_repository(
    'https://github.com/myorg/myrepo.git',
    '/tmp/foo,
    callbacks=callbacks)

calebgroom avatar Jan 26 '16 08:01 calebgroom

@calebgroom , it works well in my machine with fast speed. Originally I try gittle library to clone 'git@git...." format, it works, but the clone speed is very slow. So I try pygit2 with 'git@git...': git@git pygit2.Git Error: Unsupported URL protocol

pygit2.UserPass() works very well for me. Thank you!

swordfly avatar May 13 '16 13:05 swordfly

Using the example code above I continuously get

_pygit2.GitError: Too many redirects or authentication replays

I'm using pygit 0.24.1 with github enterprise.. any insight would be a huge help.

ScottDillman avatar Aug 03 '16 19:08 ScottDillman

im also getting the same error _pygit2.GitError: Too many redirects or authentication replays

gautam-shipt avatar Feb 19 '21 20:02 gautam-shipt

Found a solution to the Too many redirects or authentication replays issue when using personal access tokens AND when using a Github App installation token generated from the app pem key.

auth_method = 'x-access-token'
callbacks = pygit2.RemoteCallbacks(pygit2.UserPass(auth_method, token))
pygit2.clone_repository(control_repo_url, repo_tmp_path, callbacks=callbacks)

sfdc-afraley avatar Apr 06 '21 15:04 sfdc-afraley