63

I am trying to install a sample package from my github repo: https://github.com/jpmarindiaz/samplepkg

I can install it when the repo is public using any of the following commands through the R interpreter:

  • install_github("jpmarindiaz/rdali")
  • install_github("rdali",user="jpmarindiaz")
  • install_github("jpmarindiaz/rdali",auth_user="jpmarindiaz")

But when the git repository is private I get an Error:

Installing github repo samplepkg/master from jpmarindiaz
Downloading samplepkg.zip from     
https://github.com/jpmarindiaz/samplepkg/archive/master.zip
Error: client error: (406) Not Acceptable

I haven't figured out how the authentication works when the repo is private, any hints?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
jpmarindiaz
  • 1,599
  • 1
  • 13
  • 21
  • 2
    Just a guess, Did you try to set the password argument? – agstudy Jan 16 '14 at 20:02
  • sounds like a good hint! – rawr Jan 16 '14 at 20:22
  • 1
    @agstudy What I really hate about this, is that I have to enter the password in clear text (and eventually it'll be saved in `.Rhistory`). Any ideas how to avoid this? – Beasterfield Jan 17 '14 at 01:16
  • @agstudy haha how could I missed it... it'd be great not to type the password in clear text! – jpmarindiaz Jan 17 '14 at 12:21
  • 1
    @Beasterfield & jpmarindiaz I don't have a private repository to test , But I would clone my repository , using `git clone` or better using manually `Rstudio` (create a new package from an existing git repository) , then you install using `install()` from `devtools. – agstudy Jan 17 '14 at 12:34

3 Answers3

54

Have you tried setting a personal access token (PAT) and passing it along as the value of the auth_token argument of install_github()?

See ?install_github way down at the bottom (Package devtools version 1.5.0.99).

Gabi
  • 1,303
  • 16
  • 20
  • 9
    Make sure to tick the `repo scope` button or devtools will not be able to install! http://stat545.com/packages05_foofactors-package-02.html – jsta Sep 30 '16 at 16:51
  • 1
    Since this link is broken above: You can look at step 7 in selecting the scope when making a PAT https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token – micstr Jul 28 '20 at 12:05
20

Create an access token in: https://github.com/settings/tokens

Save it (Add it) to .Renviron file on the getwd() folder, this way:
GITHUBTOKEN=tokenstring
Add .Renviron to .gitignore file if your folder is a repository

Or add your tokenstring system-wide to this file if pertinent:
file.path(R.home(),"etc", "Renviron.site")

Restart R session, to get .Renviron and/or Renviron.site files loaded automatically

Check the branch name and pass it to ref

devtools::install_github("user/repo",
                         ref = "main",
                         auth_token = Sys.getenv("GITHUBTOKEN")                          
                        )
Ferroao
  • 3,042
  • 28
  • 53
6

A more modern solution to this problem is to set your credentials in R using the usethis and credentials packages.

#set config
usethis::use_git_config(user.name = "YourName", user.email = "your@mail.com")

#Go to github page to generate token
usethis::create_github_token() 

#paste your PAT into pop-up that follows...
credentials::set_github_pat()

#now remotes::install_github() will work
remotes::install_github("username/privaterepo")

More help at https://happygitwithr.com/common-remote-setups.html#common-remote-setups