【问题标题】:BitBucket API download src file zip issueBitBucket API 下载 src 文件 zip 问题
【发布时间】:2015-05-13 01:39:59
【问题描述】:

我正在连接到 BitBucket API,并希望能够将存储库的 zip 文件下载到服务器。由于存储库是私有的,因此需要用户访问。

我可以通过链接中的用户登录详细信息下载文件:

https://{user}:{pass}@bitbucket.org/{owner_name}/{repository}/get/master.zip

但我希望能够通过 API 使用访问令牌下载文件,很可能是通过 cURL 或任何其他方式。

【问题讨论】:

    标签: php zip gzip bitbucket-api


    【解决方案1】:

    我一直在使用这个函数从 bitbucket 下载一个仓库:

    function download($url, $destination) {
        try {
            $fp = fopen($destination, "w");
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, USERNAME . ":" . PASSWORD);
            curl_setopt($ch, CURLOPT_FILE, $fp);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            $resp = curl_exec($ch);
    
            if(curl_errno($ch)){
                throw new Exception(curl_error($ch), 500);
            }
    
            $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if ($status_code != 200) {
                throw new Exception("Response with Status Code [" . $status_code . "].", 500);
            }
        }
        catch(Exception $ex) {
            if ($ch != null) curl_close($ch);
            if ($fp != null) fclose($fp);
            throw new Exception('Unable to properly download file from url=[' + $url + '] to path [' + $destination + '].', 500, $ex);
        }
        if ($ch != null) curl_close($ch);
        if ($fp != null) fclose($fp);
    }
    

    【讨论】:

      【解决方案2】:

      我发现通过使用 ssh 克隆存储库非常有效。通过已经生成了一个 ssh 密钥,然后通过对 Bitbucket API ssh-key 执行 POST,我可以不受任何限制地使用 git 命令。我还必须允许 .ssh/known_hosts 文件中的 Bitbucket.org 获得访问权限。

      总的来说,如果您想从存储库访问 src 文件,此方法是最好的也是唯一的方法。

      【讨论】:

      • 只是为了补充我的回复。这仅适用于一个帐户,因为 SSH 密钥不能用于多个 BitBucket 帐户。为了解决这个问题,我不得不使用 BitBucket 的 OAuth 2,它让我可以通过提供的访问令牌克隆一个存储库。
      猜你喜欢
      • 2023-04-10
      • 2013-11-02
      • 2012-10-14
      • 1970-01-01
      • 2021-02-25
      • 2021-09-10
      • 1970-01-01
      • 2018-12-21
      • 2021-01-07
      相关资源
      最近更新 更多