Forum Discussion

alexanderg's avatar
alexanderg
Advocate II
10 years ago

authentication trought javascript

Hi everyone!

 

anyone knows how to make the authentication process by javascript, because the example of the documentation is done in C#. If someone has something would be helpful! thanks

5 Replies

  • fso's avatar
    fso
    Advocate II

    Hi, I have done this using Google Apps Script, which is basically Javascript.
    I am not a developer, so I am sure there are better/different ways, but it works, and maybe it will point you in the right direction.
    To get a valid refresh token I have to manually login once.

    function pbiInitialAuth(){
    var base = "https://login.windows.net/common/oauth2/authorize";
    var rtype = "?response_type=code";
    var clientID = "&client_id=" + pbiClientID();
    var resource = "&resource=https://analysis.windows.net/powerbi/api&redirect_uri=http://localhost:13526/Redirect";
    var clientSecret = "&client_secret=" + pbiClientSecret();
    var url = base + rtype + clientID + resource + clientSecret;
    Logger.log(url);
    }

    The url variable contains a URL that you can open in Firefox and enter your login details.
    The URL you will be redirected to contains a parameter &code=... This is the refresh token.
    Once you have that, you can automatically get new access tokens by using something like this:

    function pbiAccessToken(){
      
      var code = pbiRefreshTokenValue();
      var clientID = pbiClientID();
      var clientSecret = pbiClientSecret();
      var url = "https://login.microsoftonline.com/common/oauth2/token";
      var redirect = "http://localhost:13526/Redirect";
      
      var payload =
       {
         "client_id" : clientID,
         "client_secret" : clientSecret,
         "code": code,
         "grant_type": "authorization_code",
         "redirect_uri": redirect
       };
      
      
      var headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
      };
      
      
      var options = {
        'method': 'post',
        'headers': headers,
        'payload': payload
      };
      
      var response = UrlFetchApp.fetch(url, options);
      var data = JSON.parse(response);
      return data.access_token;
    }

    Hope that helps, it works for me.

    • JasonDunbar's avatar
      JasonDunbar
      Advocate II

      We've (our org.) just finished implementing this very recently.

       

      While I may not be able to share the code with you, I could at least try to share the architecture, setup and packages used. Bear with me and I'll get back to you with something.

       

      J

    • alexanderg's avatar
      alexanderg
      Advocate II

      Hi, fso thanks for your reply is very helpful. I am in the same situation as you because I'm not a developer, but I understand that your first function creates the link to go to the login site and enter the login credentials for get the access token. but my question is how do to save the access token generated in a variable?

      • fso's avatar
        fso
        Advocate II

        alexanderg, once you successfully logged in, you get redirected to a url that contains a parameter &code=...
        This code after the equal symbol is the refresh token. I just manually copy it and store it hardcoded in a variable like

        var refreshToken = "AAAAbbbCCC-aaaa-ssss--dddd";

        I do not have a programmatic solution to this, but also found that since you can request new access tokens with that refresh token, I never had to re-enter the code again. So for me, it just keeps working.