Forum Discussion
authentication trought javascript
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.
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
- alexanderg10 years agoAdvocate II
thanks JasonDunbar, any contribution would be very useful!