如何配置私有Bitbucket存储库,以便我可以在composer.json中包含一个私有包?

时间:2017-08-08 13:56:58

标签: git oauth composer-php bitbucket

我在Git中有一个私有的Bitbucket.org存储库。它由Laravel项目组成,可以用作composer package。我不想发布它,我只想通过将其包含在Laravel中来将其用于我的其他composer.json项目。我搜索了很多关于stackoverflow的教程和问题/答案,但我仍然无法做到这一点。以下是我的composer.json文件中应该相关的部分:

"require": {
    "php": ">=5.6.4",
    "myprovider/mypackage": "*",
    "laravel/framework": "5.4.*",
    "laravel/tinker": "~1.0"
},
"repositories": {
    "myrepository": {
        "type": "vcs",
        "url": "https://bitbucket.org/me/myrepository/"
    }
},

我尝试composer require myprovider/mypackage:*,但后来收到错误Could not fetch https://api.bitbucket.org/2.0/repositories/me/myrepository?fields=-project%2C-owner, please create a bitbucket OAuth token to access private repos。由于我之前实际创建了OAuth令牌,因此我粘贴了所需的Consumer KeyConsumer Secret,但之后我得到了这个:

Invalid OAuth consumer provided.
This can have two reasons:
1. You are authenticating with a bitbucket username/password combination
2. You are using an OAuth consumer, but didn't configure a (dummy) callback url

Installation failed, reverting ./composer.json to its original content.

  [Composer\Downloader\TransportException]
  The "?pagelen=100&fields=values.name%2Cvalues.target.hash%2Cnext&sort=-target.date" 
file could not be downloaded: failed to open stream: Resource temporarily unavailable

我是Bitbucket的新手,我怎么知道我是否正确配置了所有内容?

一步一步的配置会非常棒,我在网上找到的所有内容都不适合我的情况。

1 个答案:

答案 0 :(得分:2)

以下答案是我按照this blog post的说明并看到其有效后的个人笔记。

非常感谢gizmola的博客文章和gview的评论。

比方说,对于您的项目my-name/my-project,您想要将托管在myprovider/my-private-package的私有程序包托管在bitbucket上。

  • 登录到bitbucket
  • bitbucket settings> access management> oauth
  • 添加oauth使用者

    • 名称:作曲家
    • 回调:(必需,但未使用)http://example.com
    • 勾选:这是私人消费者
    • 勾选:permissions> repositories> read
    • 保存
  • composer oauth consumer / user for private repos

  • 获取密钥和机密
  • 打开/创建~/.composer/auth.json,并确保它具有替换xxxxxyyyyy

    的位置
    {
        "bitbucket-oauth": {
            "bitbucket.org": {
                "consumer-key": "xxxxx",
                "consumer-secret": "yyyyyy"
            }
        }
    }
    
  • 将您的包添加到composer.json

    {
       "name" : "my-name/my-project",
       "description" : "my project",
       "repositories": [
           {
             "type": "git",
             "url":  "https://bitbucket.org/myprovider/my-private-package.git"
           }
       ],
       "require": {
           "myprovider/my-private-package": "*"
        }
    }
    

请记住,如果私有包的composer.json没有设置minimum-stability(例如,设置为dev),它将无法正常工作。

myprovider/my-private-package的composer.json示例

{
    "name": "myprovider/my-private-package",
    "description": "my private package",
    "keywords": ["private package", "private", "package"],
    "type": "package",
    "version": "1.0.0",
    "minimum-stability": "dev",
    "license": "MIT",
    "authors": [
        {
          "name": "John Doe",
          "email": "john.doe@example.com"
        }
    ],
    "autoload": {"psr-0": {"": "src"}},
    "require-dev": {
        "behat/behat": "^3.4"
    }
}