cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
hsheikhali Frequent Visitor
Frequent Visitor

Re: Cannot get access token.

Here is my solution:

 

const axios = require('axios');
const adal = require('adal-node');


const getAccessToken = () => {
  const config = {
    username: 'Your Username',
    password: 'Your Password',
    clientId: 'Your Client ID',
    resource: 'https://analysis.windows.net/powerbi/api'
  }

  const authority = 'https://login.microsoftonline.com/Your Tenant'; 
  // Tenant can be found in the Azure Portal.
  // When you access Azure AD, it should show your the name of your tenant.
  // Don't have a link but you can figure out where to get this with some googling.


  let context = new adal.AuthenticationContext(authority, true)
  const callback = (err, accessToken) => {
    if (!err) {
      console.log(accessToken.accessToken); // Returns access token.
      makeApiCall(accessToken.accessToken); --> This is my actual API call.
    } else {
      console.log(err);
    }
  }
  context.acquireTokenWithUsernamePassword(
    config.resource,
    config.username,
    config.password,
    config.clientId,
    callback
  );
}

const makeApiCall = (token) => {
  const config = {
    headers: {
      'Authorization': "Bearer " + token,
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept': 'application/json'
    }
  };

// These options are only used when generating a embedToken.
//{ //accessLevel:"View", //allowSaveAs:"false" //}, const url = 'https://api.powerbi.com/v1.0/myorg/groups/5cfa7118-e1eb-4008-afe7-239e5286b9f1/reports/'; axios.get(url, config) .then(response => { let result = response.data; console.log(result); }) .catch(err => { console.log(err); console.log(err.response.status); console.log(err.response.statusText); }) } getAccessToken();

View solution in original post

Re: Cannot get access token.

i am also facing the similar issue, did you able to figured it out through postman. Please let me know te root cause.

Highlighted
NEbdf Frequent Visitor
Frequent Visitor

Re: Cannot get access token.

Same issue for me since over half a year now. No solution in sight besides MS C# demo.

Helpful resources

Announcements
Coming Soon: T-Shirt Design Contest

Coming Soon: T-Shirt Design Contest

Keep your eyes open for our upcoming T-shirt design contest!

Meet the 2020 Season 1 Power BI Super Users!

Meet the 2020 Season 1 Power BI Super Users!

It’s the start of a new Super User season! Learn all about the new Super Users and brand-new tiered recognition system.

Super User Challenge: Can You Solve These?

Super User Challenge: Can You Solve These?

We're celebrating the start of the New Super User season with our first ever Super User 'Can You Solve These?' challenge.

Power BI Desktop Update - February 2020

Power BI Desktop Update - February 2020

We are super excited for our update this month, as we are releasing two of our top community requests!

Power Platform Online Conference

Power Platform Online Conference

Join us for the first ever Power Platform Online Conference!

Top Solution Authors
Top Kudoed Authors