rvest login with 2FA

I have some code I have used in the past to count student commits on GitHub (see this blogpost), which worked as of a few months ago. However, in the interim I have enabled two-factor authentication on GitHub. So I'm now running into an issue when I attempt to use rvest to log in to GitHub. Does anyone have demo code of how to get around this? I suspect it is something like

library(rvest)

session <- session("https://github.com/login")

login <- session %>%
  html_node("form")%>%
  html_form()%>%
  html_form_set(login = "YourGitHubUsername", password = "SuperSecureP@ssw0rd")


github <- session %>%
  session_submit(login, submit = "commit") %>%
  session() %>%
  html_node("form") %>%
  html_form() %>%
  html_form_set(authenticity_token = "2FANUMBERS") %>%
  session_submit(login, submit = "submit") %>%
  read_html()

although I get an error running this, perhaps because I haven't pre-specified the URL where the login will bring you? If anyone has an example of using rvest with two-factor authentication, please let me know!

In case anyone runs across this, I was able to solve my issue. The main problem with my code above is I thought the box to enter the second factor number was labeled authenticity_token when really it should be otp. This code works:

library(rvest)

session <- session("https://github.com/login")

login <- session %>%
  html_node("form")%>%
  html_form()%>%
  html_form_set(login = "YourGitHubUsername", password = "SuperSecureP@ssw0rd")


github <- session %>%
  session_submit(login, submit = "commit") 

twofac <- github %>%
  html_node("form") %>%
  html_form() %>%
  html_form_set(otp = "2FANUMBERS")

done <- github %>%
  session_submit(twofac) %>%
  read_html()

Of course, you need to substitute in YourGitHubUsername and SuperSecureP@ssw0rd, as well as the 2FANUMBERS (make sure to watch the app and start right at the top of a 30-second period to give yourself the most time!). Thanks to this post from Petr Bouchal for giving me the confidence that I could submit more than one form in a session!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.