cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
hsheikhali
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

Highlighted
narendra09451
New Member

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.

NEbdf
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
New Ranks Launched March 24th!

New Ranks Launched March 24th!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power BI Community!

‘Better Together’ Contest Finalists Announced!

‘Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

Arun 'Triple A' Event Video, Q&A, and Slides

Arun 'Triple A' Event Video, Q&A, and Slides

Missed the Arun 'Triple A' event or want to revisit it? We've got you covered! Check out the video, Q&A, and slides now.

Top Solution Authors