Mark Mark - 3 months ago 15
R Question

R - curl Github API to access private repos

All I'm trying to do is read all the repos and issues in my organizations private repos. I can from my Windows 7

cmd.exe
execute

curl -u "user:pass" https://api.github.com/orgs/:org/repos


and I get back all of my repositories. I can pipe this to a file:

curl -u "user:pass" https://api.github.com/orgs/:org/repos > "C:\Users\Location\file.txt"


and this saves the JSON output. I can replicate this in R but in what seems like a terrible way.

fullRepos = system('curl -s -u "user:pass" -G https://api.github.com/orgs/:org/repos',
intern=T,show.output.on.console = F)


This captures the output (
intern = T
) and the
-s
gets rid of the progress bars so I can collapse the lines and turn it into a data frame. This gets back all the repositories, public and private.

I tried using
RCurl
to do the same thing but the code below only provides the public repositories. The
httpheader
is because otherwise it the API rejects my call.

RCurl::getURL(url="https://api.github.com/orgs/:org/repos",userpwd ="user:pass",
httpheader = c('User-Agent' = "A user agent"))


I also tried
httr
and it also only provides the public repositories.

httr::GET(url="https://api.github.com/orgs/:org/repos",userpwd="user:pass")


What am I doing wrong with
RCurl
and
httr
? I'd rather have a workflow that doesn't make a system command and then paste the lines together.

Answer

We can use the authenticate() helper function in httr to build the authentication header for us w/o having to manually create it. Also, verbose() can be used to debug HTTP issues:

httr::GET(url="https://api.github.com/orgs/:‌​org/repos",
          httr::authenticate("user", "pass"), 
          httr::verbose())