For websites that require
authentication, you can use Chromote to get screenshots by doing the
following:
There are two ways to capture the cookies.
Method 1: Manually interact with the page
The first method uses the headless browser’s viewer. This can be a
bit inconvenient because it requires going through the entire login
process, even if you have already logged in with a normal browser.
First navigate to the page:
library(chromote)
b <- ChromoteSession$new()
b$view()
b$Page$navigate("https://beta.rstudioconnect.com/content/123456/")
Next, log in interactively via the viewer. Once that’s done, use
Chromote to capture the cookies.
cookies <- b$Network$getCookies()
str(cookies)
saveRDS(cookies, "cookies.rds")
After saving the cookies, you can restart R and navigate to the page,
using the cookies.
library(chromote)
b <- ChromoteSession$new()
b$view()
cookies <- readRDS("cookies.rds")
b$Network$setCookies(cookies = cookies$cookies)
# Navigate to the app that requires a login
b$Page$navigate("https://beta.rstudioconnect.com/content/123456/")
b$screenshot()
Method 2: Capture and re-use cookies
The second method captures the cookies using a normal browser. This
is can be more convenient because, if you are already logged in, you
don’t need to do it again. This requires a Chromium-based browser, and
it requires running DevTools-in-DevTools on that browser.
First, navigate to the page in your browser. Then press CMD-Option-I
(Mac) or Ctrl-Shift-I (Windows/Linux). The developer tools panel will
open. Make sure to undock the developer tools so that they are in their
own window. Then press CMD-Option-I or Ctrl-Shift-I again. A second
developer tools window will open. (See this
SO answer for detailed instructions.)
In the second developer tools window, run the following:
var cookies = await Main.sendOverProtocol('Network.getCookies', {})
JSON.stringify(cookies)
This will return a JSON string representing the cookies for that
page. For example:
[{"cookies":[{"name":"AWSALB","value":"T3dNdcdnMasdf/cNn0j+JHMVkZ3RI8mitnAggd9AlPsaWJdsfoaje/OowIh0qe3dDPiHc0mSafe5jNH+1Aeinfalsd30AejBZDYwE","domain":"beta.rstudioconnect.com","path":"/","expires":1594632233.96943,"size":130,"httpOnly":false,"secure":false,"session":false}]}]
Copy that string to the clipboard. In your R session, you can paste
it to this code, surrounded by single-quotes:
cookie_json <- '[{"cookies":[{"name":"AWSALB","value":"T3dNdcdnMasdf/cNn0j+JHMVkZ3RI8mitnAggd9AlPsaWJdsfoaje/OowIh0qe3dDPiHc0mSafe5jNH+1Aeinfalsd30AejBZDYwE","domain":"beta.rstudioconnect.com","path":"/","expires":1594632233.96943,"size":130,"httpOnly":false,"secure":false,"session":false}]}]'
cookies <- jsonlite::fromJSON(cookie_json, simplifyVector = FALSE)[[1]]
Then you can use Chromote to navigate to the page and take a
screenshot.
library(chromote)
b <- ChromoteSession$new()
b$Network$setCookies(cookies = cookies$cookies)
b$Page$navigate("https://beta.rstudioconnect.com/content/123456/")
b$screenshot()