rsconnect cannot publish to server when account is setup programmatically with API Keys

344 views Asked by At

I have a setup with an RStudio Workbench and Connect server, where I want to automatically publish content to Connect.

When I connect to the connect server manually (clicking the blue share button, authenticating etc, and then publishing the content) I can then programmatically re-publish/update my app like this

deployApp(
  appDir = app_dir,   # app/
  appName = app_name, # test-app
  appId = app_id,     # 123 as noted from the manual step; app already exists with this ID
  account = RSCONNECT_USER, # myuser
  launch.browser = FALSE,
  forceUpdate = TRUE,
  server = RSCONNECT_SERVER # the connect-server address
)
#> Preparing to deploy application...DONE
#> Uploading bundle for application: 123...DONE
#> Deploying bundle: xyz for application: 123 ...
#> ...
#> Application successfully deployed to https://myconnect-server.com/content/...

However, I eventually want to run this in a CICD job, therefore I cannot manually create the account/connection to the Connect server, instead I want to connect using an API key using the connectApiUser() function.

But when I try the following I get an error on the deployApp() call:

# not real info ofc
RSCONNECT_USER   <- "myuser"
RSCONNECT_SERVER <- "myconnect-server.com"
APIKEY           <- "mysecret (working!) API-Key"

app_id           <- "123"
app_name         <- "test-app"
app_folder       <- "app"

library(rsconnect)
packageVersion("rsconnect") # latest version as of 2023-06-28
#> [1] ‘0.8.29’


## 1 connect to the server and add user ====

removeServer(RSCONNECT_SERVER)
#> Warning... The server 'myconnect-server.com' is not currently registered

addServer(paste0("https://", RSCONNECT_SERVER), RSCONNECT_SERVER)
#> Server 'myconnect-server.com' added successfully: https://myconnect-server.com


# Note the error is expected as the user is not yet registered.
removeAccount(RSCONNECT_USER)
#> Error... account named 'myuser' does not exist

connectApiUser(
  account = RSCONNECT_USER,
  server = RSCONNECT_SERVER,
  apiKey = APIKEY
)
#> Account registered successfully: myuser



## 2 check connections ====

servers()[, c("name", "url")]
#>                    name                           url
#> 1  myconnect-server.com  https://myconnect-server.com
#> 2          shinyapps.io   https://api.shinyapps.io/v1
#> 3           posit.cloud   https://api.shinyapps.io/v1

accounts()
#>     name                server
#> 1 myuser  myconnect-server.com


## 3 Deploy to the server ====

deployApp(
  appDir = app_folder,
  appName = app_name,
  appId = app_id,
  account = RSCONNECT_USER,
  launch.browser = FALSE,
  forceUpdate = TRUE,
  server = RSCONNECT_SERVER
)
#> Preparing to deploy application...Error: HTTP 404
#> GET https://myconnect-server.com/applications/123
#> 404 page not found

Am I understanding the way rsconnect want to use connectApiUser() wrongly or why does this not work?

Note that this official bash script does not work for me as I need more flexibility than the script provides, therefore I want to use the rsconnect package as outlined above.

Update I opened an issue with rsconnect.

1

There are 1 answers

0
David On

As mentioned in the GitHub Issue, the solution was to specify the server URL slightly differently.

Instead of using

addServer(paste0("https://", RSCONNECT_SERVER), RSCONNECT_SERVER)

(which resulted in the error), using the following solved the issue:

addServer(paste0("https://", RSCONNECT_SERVER, "/__api__"), RSCONNECT_SERVER)