Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
alexsilvar
Frequent Visitor

How to Generate Embed Token in pure JavaScript

Hello 🙂

 

I already did the autentication steps and already can get the access_token. I made this using the ADAL.js

Now I'm trying to simply generate a embed token, I already got all the other informations.

With this first code, I can get the access_token, embedUrl, webUrl, reports, dashboards, groups, even groups/dashboards or reports:

ADAL.acquireToken("https://analysis.windows.net/powerbi/api",function(error, token){
		access_token = token;
		if (error || !token) {
                console.log('ADAL Error Occurred: ' + error);
                return;
        }

		var trythis = "Bearer " + token;
		//alert(trythis);
		var request = new XMLHttpRequest();
		//Parametros para selecionar o que vai ser mostrado
		var type = 'reports';//dashboards
		//var group = 'me';
		var thingName = 'DashboardTeste';
		
		request.open('GET', 'https://api.powerbi.com/v1.0/myorg/'+ type);

		request.setRequestHeader('Authorization', trythis);

		request.onreadystatechange = function (reposta) {
			if (this.readyState === 4) {
				console.log('Status:', this.status);
				console.log('Body:', this.responseText);
				handleEmbed(type,this.responseText,thingName);
			}
		};

		request.send();

		});

When I try to generate a Embed Token like in this code below, it returns me a 401 error:

 

ADAL.acquireToken("https://analysis.windows.net/powerbi/api",function(error, token){
		var request = new XMLHttpRequest();
		
		request.open('POST', 'https://api.powerbi.com/v1.0/myorg/groups/'+ groupId+'/'+type+'/'+typeId+'/GenerateToken',true);
	
		request.setRequestHeader('Content-Type','application/json; charset=utf-8');
		request.setRequestHeader('Authorization', 'Bearer '+ token);
		
		
		request.onreadystatechange = function (reposta) {
			if (this.readyState === 4) {
				console.log('Status:', this.status);
				console.log('Body:', this.responseText);
			}
		};

		var str = {
			accessLevel:"View"
		};
		request.send(JSON.stringify(str));		
	});

Please, someone help me.

1 ACCEPTED SOLUTION
eligr
Power BI Team
Power BI Team

Hi.

You cannot use 'me' as a group for my workspace, generate token works only in groups

 

https://msdn.microsoft.com/en-us/library/mt784614.aspx

 

You can use your Pro license for developing, when you go to production, you need to purchase a capacity:

 

https://docs.microsoft.com/en-us/power-bi/developer/embedded-faq

 

 

Eli.

View solution in original post

4 REPLIES 4
mittalpatel130
Helper III
Helper III

Hello @alexsilvar  - 

 

Now I'm also in the same boat.. We are embedding the reports in an application which only supports Client Side Scripting. 

Did this code work for you to generate new Embed Token? If yes, could you please post here the full code.. 

 

It will be much appreciated.. Thank you.

eligr
Power BI Team
Power BI Team

Hi.

You cannot use 'me' as a group for my workspace, generate token works only in groups

 

https://msdn.microsoft.com/en-us/library/mt784614.aspx

 

You can use your Pro license for developing, when you go to production, you need to purchase a capacity:

 

https://docs.microsoft.com/en-us/power-bi/developer/embedded-faq

 

 

Eli.

v-ljerr-msft
Employee
Employee

Hi @alexsilvar,

 

Based on my test, an incorrect group_id could cause the 401 error. 

 

If the group is correct, a fiddler capture may be required to investigate further. The required permission scope may be missing for the registered application within Azure AD. Verify the required scope is present within the app registration for Azure AD within the Azure portal. Smiley Happy

 

Regards

Hi @v-ljerr-msft

 

Do you know if i can use the group 'me' as a group id? When I try it gives me the same error.

And I don't know if its relevant but I need a specific account type? (I'm using Power BI Pro)

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

Check out the June 2024 Power BI update to learn about new features.

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors