通过 HTTPS 使用带有私有 GitHub 存储库的 Composer

2022-01-21 00:00:00 git github php composer-php

我们有许多私有存储库,我们希望将它们包含在使用 Composer 的 PHP 应用程序中.composer.json 文件包含这个条目,它定义了我们的第一个私有存储库:

We have a number of private repositories which we would like to include in a PHP application using Composer. The composer.json file contains this entry which defines our first private repository:

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/vendor/package.git"
    }
]

然后我们像往常一样需要存储库:

We then require the repository like normal:

"require": {
    "vendor/package": "~1.0.0"
}

我做的唯一额外的事情是在 GitHub 上设置一个私有访问令牌并将其存储在 composers auth.json 文件中.看起来像:

The only extra thing I have done is set a private access token up on GitHub and store it in composers auth.json file. That looks like:

{
    "github-oauth": {
        "github.com": "<my_access_token>"
    }
}

一切看起来都已正确设置,Composer 可以使用访问令牌通过 HTTPS 访问 GitHub,但我们收到的错误如下所示:

Everything looks like it's been setup correctly for Composer to access GitHub via HTTPS with the access token, but we're getting errors which look like this:

[RuntimeException]
Failed to clone https://github.com/vendor/package.git via https protocols, aborting.
 - https://github.com/vendor/package.git
   Cloning into bare repository '/home/vagrant/.composer/cache/vcs/https---github.com-vendor-package.git'...
   remote: Invalid username or password.
   fatal: Authentication failed for 'https://github.com/vendor/package.git/'

访问令牌是有效的,因为我已经使用它超过了您经常看到的超出速率限制的消息,并且它在 GitHub 上显示为最近使用.

The access token is valid, because I have used it to get past the rate limit exceeded message you often see and it's showing up on GitHub as being recently used.

我们在这里错过了一步吗?理想情况下,我们希望仅使用 Composer 和 GitHub 来完成此操作.设置 Satis 或 Trojan 并不是我们热衷于做的事情.从文档看来,我们应该能够使用个人访问令牌访问我们的私有仓库.

Have we missed out a step here? Ideally we'd like to do this using just Composer and GitHub. Setting up Satis or Trojan isn't something we're keen on having to do. It seems from the documentation that we should be able to access our private repos using a personal access token.

推荐答案

不要使用 HTTPS,而是使用以下语法:

Instead of HTTPS, use this syntax:

"repositories": [
    {
        "type": "vcs",
        "url": "git@github.com:vendor/package.git"
    }
]

我们经常成功地使用它.

We use this quite often, successfully.

请参见此处:https://getcomposer.org/doc/05-repositories.md#using-private-repositories

相关文章