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.
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?
- fso10 years agoAdvocate 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 likevar 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.