0

I am trying to import the javascript MSAL library into an rMarkdown script so that I can use it to obtain an oauth token for a db connection.

The first script called is to import the msal library. And the second one is the script used to obtain the oauth token. However I'm assuming it errors at the first script as the "msal library is not defined". Where am I going wrong?

Here is the minimal reproducible script:

---
title: "Test"
output: 
 flexdashboard::flex_dashboard:
 orientation: rows
 vertical_layout: scroll
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(htmltools)
# serve the files
addResourcePath(prefix = "files", directoryPath = "assets")

htmltools::tagList(
 htmltools::tags$script(src = c(href = "https://alcdn.msauth.net/browser/2.17.0/js/msal-browser.js")),
 htmltools::tags$script("

   async function wrapperFunc() {
                   const msalConfig = {
                       auth: {
                           clientId: 'XXX',
                           authority: 'XXX'
                       }
                   };
                
                   const msalInstance = new msal.PublicClientApplication(msalConfig);
               
                   const silentRequest = {
                       scopes: ["XXX]
                   };
                   
                   const callLogin = async function(silentRequest, msalInstance) {
                       try {
                           const loginResponse = await msalInstance.loginPopup(silentRequest);
                           return loginResponse;
                       } catch (err) {
                           console.log(err)
                       }
                   }
                   
                   response = callLogin(silentRequest, msalInstance);
           return response;
   }                
   wrapperFunc().then(result => {
                   console.log(result['accessToken']);
   });
   
   ")
   )

Dieu94
  • 371
  • 1
  • 11
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Does the code in the second script wait for an on-load event? Or when does the code run? Are you sure it runs after the library is loaded? – MrFlick May 15 '23 at 15:23
  • Hi @MrFlick, I've edited the code to include the JS script, instead of just referencing it. All I need it to do at this stage is print the oauth token in the console, however I am getting a "msal is not defined" error. Both scripts work when I embed it into a shiny app in the rmarkdown document but I am trying to avoid Shiny! – Dieu94 May 15 '23 at 15:34
  • You don't have to put `c( href =`. – Stéphane Laurent May 15 '23 at 16:02
  • Thanks @StéphaneLaurent, but I still get the same error :( – Dieu94 May 15 '23 at 16:21

1 Answers1

0

I would try something like this. But since I don't have your tokens I can't check.

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
```

<script src="https://alcdn.msauth.net/browser/2.17.0/js/msal-browser.js"></script>

```{js}
async function wrapperFunc() {
  const msalConfig = {
    auth: {
      clientId: "XXX",
      authority: "XXX"
    }
  };

  const msalInstance = new msal.PublicClientApplication(msalConfig);

  const silentRequest = {
    scopes: ["XXX"]
  };

  const callLogin = async function (silentRequest, msalInstance) {
    try {
      const loginResponse = await msalInstance.loginPopup(silentRequest);
      return loginResponse;
    } catch (err) {
      console.log(err);
    }
  };

  response = callLogin(silentRequest, msalInstance);
  return response;
}
wrapperFunc().then((result) => {
  console.log(result["accessToken"]);
});
```
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225