如何使用 R從GitHub私有倉庫的特定分支下載單個檔案?
默認分支可以輕松完成,例如:
require(httr)
github_path = "https://api.github.com/repos/{user}/{repo}/contents/{path_to}/{file}"
github_pat = Sys.getenv("GITHUB_PAT"))
req <- content(GET(github_path,
add_headers(Authorization = paste("token", github_pat))), as = "parsed")
tmp <- tempfile()
r1 <- GET(req$download_url, write_disk(tmp))
...但我不知道如何為特定分支做到這一點。試圖在其中包含分支名稱,github_path
但它不起作用(Error in handle_url(handle, url, ...)
)。
因為使用 classic 很容易curl
,例如:
curl -s -O https://{PAT}@raw.githubusercontent.com/{user}/{repo}/{branch}/{path_to}/{file}
...我試著這樣做:
tmp <- tempfile()
curl::curl_download("https://{PAT}@raw.githubusercontent.com/{user}/{repo}/{branch}/{path_to}/{file}", tmp)
但它也沒有奏效。
我錯過了什么?謝謝!
uj5u.com熱心網友回復:
您可以像這樣在 R 中使用 curl 來包含 auth 標頭和所需檔案的路徑:
library(curl)
h <- new_handle(verbose = TRUE)
handle_setheaders(h,
"Authorization" = "token ghp_XXXXXXX"
)
con <- curl("https://raw.githubusercontent.com/username/repo/branch/path/file.R", handle = h)
readLines(con)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/470888.html
標籤:r github github-api