async getEmbedToken(workspaceId, reportId) {
// Generate accessToken
const tokenEndpoint = https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token
;
const postData = {
grant_type: “client_credentials”,
client_id: clientId,
client_secret: clientSecret,
scope: “https://analysis.windows.net/powerbi/api/.default”,
};
const tokenResponse = await axios.post(
tokenEndpoint,
qs.stringify(postData),
{
headers: { "Content-Type": "application/x-www-form-urlencoded" },
}
);
const accessToken = tokenResponse.data.access_token;
console.log("accessToken------------", accessToken);
// Generate datasetId
const datasetId = await _resourceSettings.getDatasetId(
accessToken,
workspaceId,
reportId
);
// Generate the embed token
const powerBiApiEndpoint = `https://api.powerbi.com/v1.0/myorg/groups/${workspaceId}/reports/${reportId}/GenerateToken`;
const headers = {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
};
const body = {
datasets: [{ id: datasetId }],
accessLevel: "View", // Optional, defaults to View
};
const embedTokenResponse = await axios.post(powerBiApiEndpoint, body, {
headers,
});
console.log(
"embedTokenResponse--------------",
embedTokenResponse.data.token
);
return { embedToken: embedTokenResponse.data.token, datasetId: datasetId };
}
async getDatasetId(accessToken, workspaceId, reportId) {
const response = await axios.get(
https://api.powerbi.com/v1.0/myorg/groups/${workspaceId}/reports/${reportId}/datasources
,
{
headers: {
Authorization: Bearer ${accessToken}
,
},
}
);
return response.data.value[0].datasourceId;
}
I have created a workspace in power bi service and given permission for my service principal. when i create a paginated report using power bi report builder and published to power bi service it can be viewed in my workspace in my power bi service. when i try to embed the same in my react js application it will throw an error like this
{
"message": "PowerBINotAuthorizedException",
"detailedMessage": "Get report failed",
"errorCode": "401",
"level": 6,
"technicalDetails": {
"requestId": "320dea56-3278-4b5c-9ae8-ee42eced0666"
}
}
i have created service principal in azure ad and given these app permissions
App.Read.All
Dataset.Read.All
Dataset.ReadWrite.All
Report.Read.All
Report.ReadWrite.All
please look in to this issue and help me to identify the problem