Cloning repository with Oauth is rejected with 'not authorized'

Hi all,

some time ago, I wrote an article about authentication against Git repositories with JGit (a pure Java Git implementation). Now I am about to update a section about the authentication methods provided by GitLab and how they work.

From my understanding, Oauth tokens (those managed under Profile Settings > Applications) can be used to access private repositories. So I created a private repository and an Application named foo for testing. The secret generated by GitLab is a1b2c3. However, attempting to clone the repository with

git clone https://foo:a1b2c3@gitlab.com/user/repo.git

gave me this error

not authorized

I also tried these variants

git clone https://oauth2:a1b2c3@gitlab.com/user/repo.git

git clone https://a1b2c3@gitlab.com/user/repo.git

git clone https://foo@gitlab.com/user/repo.git
password: a1b2c3

But all resulted in the same error.

The experiments described here were all made with GitLab community edition (gitlab.com). Is my assumption that Oauth can be used to authenticate against repositories wrong? Or can someone give me a hint what I am doing wrong?

Thanks in advance,
Rüdiger

1 Like

This solution worked fine for me!

git clone https://oauth2:access-token@gitlab.com/user/repo.git

I don’t think the GitLab secret can be used to clone as you are attempting to do.
You have to use the id and secret to get an access-token from GitLab & then use that.

Refer:
https://docs.gitlab.com/ee/api/oauth2.html