Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
alexanderg
Advocate II
Advocate II

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 5
fso
Advocate II
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.

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?

@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.

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

____________________________________________________
My other computer is an Azure data centre.

thanks @JasonDunbar, any contribution would be very useful!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.