ropensci / qualtRics

Download ⬇️ Qualtrics survey data directly into R!
https://docs.ropensci.org/qualtRics
Other
215 stars 70 forks source link

Authenticating with multiple Qualtrics accounts #110

Closed LarryVincent closed 5 years ago

LarryVincent commented 5 years ago

I am so excited that you are updating this package. I use it every day.

I am probably a rare case, but I have two separate Qualtrics accounts. One is for work at my day job and one is for my work as a university professor. It's a little cumbersome to have to use qualtrics_api_credentials each time I start a session, but I can't embed my credentials into my environment profile because I am constantly moving back and forth between accounts. Is there perhaps a way for me to tell the package which account I want it to use when I load the package or call on its methods?

juliasilge commented 5 years ago

This is a rare case so I'm not sure I'll build in support for this to the package itself, but I have something of a workaround for you so that at least you don't have to type in or copy the really long credentials in each file, and can in fact store them in your .Renviron file, where authentication credentials belong.

So first, one time, write all your credentials to .Renviron:

Sys.setenv(
    QUALTRICS_API_KEY_UNI ="<YOUR-UNIVERSITY-QUALTRICS_API_KEY>" ,
    QUALTRICS_BASE_URL_UNI = "<YOUR-UNIVERSITY-QUALTRICS_BASE_URL>",
    QUALTRICS_API_KEY_JOB ="<YOUR-DAYJOB-QUALTRICS_API_KEY>" ,
    QUALTRICS_BASE_URL_JOB = "<YOUR-DAYJOB-QUALTRICS_BASE_URL>",
)

Only do that part one time; now your credentials are all stored as environment variables.

Now each time you have a script that uses qualtRics, include a bit of code at the beginning that grabs the right environment variables and assigns them into the environment variables for just that session. If you are working on a university project, it would be:

api_key <- Sys.getenv("QUALTRICS_API_KEY_UNI")
base_url  <- Sys.getenv("QUALTRICS_BASE_URL_UNI")
qualtrics_api_credential(api_key, base_url)

If you are working on a day job project, it would be:

api_key <- Sys.getenv("QUALTRICS_API_KEY_JOB")
base_url  <- Sys.getenv("QUALTRICS_BASE_URL_JOB")
qualtrics_api_credential(api_key, base_url)

This is definitely annoying to have this extra step, but with this approach, you won't have to save your credentials directly in code (and check them into version control, which is of course not great).

LarryVincent commented 5 years ago

This is a great work-around and I’m embarrassed 😳 I didn’t think of it myself. Thanks @juliasilge !!