Access GitLab repo with project access token

Viewed 20215

According to the documentation, it should be possible to access GitLab repos with project access tokens:

The username is set to project_{project_id}_bot, such as project_123_bot.

Never mind that that's a lie -- the actual user is called project_4194_bot1 in my case; apparently they increment a number for subsequent tokens.

Either way -- and I have tried both with and without the trailing 1 -- I would expect

git clone "https://project_4194_bot1:$PROJECT_TOKEN@my.gitlab.host/my-group/my-project.git"

to succeed, same as with my.username:$PERSONAL_TOKEN (which works perfectly). However, I get

remote: HTTP Basic: Access denied
fatal: Authentication failed for '<snip>'

What may be going on here? How can I access GitLab repositories using project access tokens?


It's not as if we'd get that far, but FWIW, the token seems to have sufficient permissions:

enter image description here

3 Answers

It seems that using the project name as username works. In your case replacing project_4194_bot1 with my-project should work:

git clone "https://my-project:$PROJECT_TOKEN@my.gitlab.host/my-group/my-project.git"

EDIT: One can actually use any non-blank value as a username (see docs), as others correctly pointed out.

From the GitLab documentation:

With Git, when using HTTP Basic Authentication, use:

  • Any non-blank value as a username.
  • The project access token as the password.

In previous GitLab versions, it would have been necessary to use project_4194_bot1 as the username. However, the current version lets you use any username you wish:

git clone "https://anything:$PROJECT_TOKEN@my.gitlab.host/my-group/my-project.git"

I've found that depending on the git version The 'user' can be left empty or a random string if the token is a project access token.

git clone "https://<$USER>:$PROJECT_TOKEN@my.gitlab.host/my-group/my-project.git"
Related