Cannot generate embed token using access token in power bi

945 views Asked by At

I'm trying to be understand authorization mechanism in power bi API

I would embed a report in my web app.

I have done the steps as mentioned in docs

Actually I would get report embedded url then use power bi JS API to embed the report.

Getting access_token is successful

var options = {
    'method': 'POST',
    'url': `https://login.microsoftonline.com/${process.env.TENANT_ID}/oauth2/token`,
    'headers': {
        'Content-Type': 'multipart/form-data'
    },
    formData: {
        'grant_type': process.env.GRANT_TYPE,
        'client_id': process.env.CLIENT_ID,
        'client_secret': process.env.CLIENT_SECRET,
        'resource': "https://analysis.windows.net/powerbi/api",
        'Scope': "https://analysis.windows.net/powerbi/api/.default"
    }
};

Now I try to get embedded token for report in group

var data = { accessLevel: "View", datasetId: "5b11d62a-803e-46c9-83f3-*****" };

var config = {
    method: 'post',
    url: `https://api.powerbi.com/v1.0/myorg/groups/${process.env.GROUP_ID}/reports/${process.env.Report_ID}/GenerateToken`,
    headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${JSON.parse(response).access_token}`
    },
    data: data
};
let embedtoken
try {
    embedtoken = await axios(config)
}
catch (e) {
    console.log(e)
}

context.res = {
    // status: 200, /* Defaults to 200 */
    body: JSON.parse(response).access_token
};

I get error 400 response

But When I generate an embed token for dashboard I get a valid token. But of course that's not working with get report API

My goal is to get report infos. For information I get get that using the access token but it's not safe

1

There are 1 answers

0
mayur_007X On

For POST API requests, data should be passed in string format. This can be done by using for example, JSON.stringify(data). Refer below code snippet which should resolve the error.

var config = {
 method: 'post',
 url: `https://api.powerbi.com/v1.0/myorg/groups/${process.env.GROUP_ID}/reports/${process.env.Report_ID}/GenerateToken`,
 headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${JSON.parse(response).access_token}`
 },
data: JSON.stringify(data) };