2015-05-13 182 views
0

我連接到BitBucket API並希望能夠將存儲庫的zip文件下載到服務器。由於存儲庫是私密的,它需要用戶訪問。BitBucket API下載src文件zip問題

我可以通過鏈接中的用戶登錄信息下載文件:

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

但是我想能夠通過使用通過API訪問令牌最有可能通過捲曲或通過下載文件任何其他手段。

回答

0

我發現通過使用ssh克隆存儲庫完美工作。通過生成已經生成的ssh密鑰,然後通過對Bitbucket API ssh-key進行POST,我可以不受任何限制地使用git命令。我還必須允許.ssh/known_hosts文件中的Bitbucket.org訪問。

總體而言,如果您希望從存儲庫訪問src文件,此方法是最好的唯一方法。

+0

只需添加到我的反應。這僅適用於一個帳戶,因爲SSH密鑰不能用於多個BitBucket帳戶。爲了解決這個問題,我不得不使用BitBucket的OAuth 2,它使用我們提供的訪問令牌克隆一個存儲庫。 – Ufb007

1

我一直在使用這個函數從到位桶下載資源庫:

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); 
}