Composer 使用 https 而不是 ssh(对于 GitLab)

Composer is using https instead of ssh (for GitLab)

当使用这样的composer.json安装私有包时:

{
    "require": {
        "foobar/example-package": "dev-master"
    },
    "repositories": [{
        "type": "vcs",
        "url": "git@gitlab.com:foobar/example-package.git"
    }]
}

出于某种原因,Composer 尝试使用 https 而不是 ssh

$ composer update
Loading composer repositories with package information
Failed to download foobar/example-package:The "https://gitlab.com/api/v4/projects/foobar%2Fexample-package" file could not be downloaded (HTTP/1.1 404 Not Found)      
Your credentials are required to fetch private repository metadata (git@gitlab.com:foobar/example-package.git)
A token will be created and stored in "…\composer\home/auth.json", your password will never be stored
To revoke access to this token you can visit https://gitlab.com/profile/applications
Username:

即使使用 -vvv 也没有任何迹象表明 Composer 需要通过 https 访问存储库 - 虽然完全可以通过 [=14 将代码推送和拉取到私有存储库=](使用存储在 SSH 代理中的私钥)。

Composer 1.x 和 2.x 会发生这种情况。有没有办法只能使用ssh这样的操作?这个问题似乎也特定于 GitLab,因为我从未在 GitHub 存储库中遇到过它。

仔细检查“Install composer packages from private repository from GitLab" from Syed Sirajul Islam Anik

So, to install a package from GitLab which is not a public repository will need a Personal Access Token.
You can issue your PAT by going to Profile Icon > Settings > Access Token. Then to issue a new PAT, write a name and select a scope api/read_api, read_api is a minimum. api will work too. If you want to set an expiry, can put value in that. Then press the Create personal access token Button.

Next, from the terminal. Run the following command.

composer config --global --auth gitlab-token.gitlab.com PAT_TOKEN

Replace the PAT_TOKEN with the token you found from the above process. And if you’re using a self-hosted GitLab, then you’ll need to change the URL gitlab.com to your self-hosted URL.

Then, in your composer.json file, add the following snippet.

"repositories": [
   {
       "type": "vcs",
       "url": "git@gitlab.com:namespace/repo-name.git"
   }
]

或者,,使用"type": "git"代替"vcs"

"repositories": [
    {
        "type": "git",
        "url": "git@gitlab.com:namespace/repo-name.git"
    }
]

If your composer.json file already contains the repository key, then add the object to it.

Finally,

composer require vendor-name/package-name

换句话说,即使 URL 是 SSH,您仍然需要 PAT(令牌)才能使用由 composer.[=26= 调用的 HTTPS GitLab API ]

感谢 @rob006vcs 更改为 git 将解决问题。

he/she 在评论中提到了这一点,也许有些人没有注意到,所以我重复它作为一个答案可能对某人有所帮助。