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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

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
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.