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

How to Generate Embed Token in pure JavaScript

Hello Smiley Happy

 

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

Accepted Solutions
Highlighted
Power BI Team eligr
Power BI Team

Re: How to Generate Embed Token in pure JavaScript

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.

3 REPLIES 3
v-ljerr-msft Super Contributor
Super Contributor

Re: How to Generate Embed Token in pure JavaScript

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

alexsilvar Frequent Visitor
Frequent Visitor

Re: How to Generate Embed Token in pure JavaScript

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)

Highlighted
Power BI Team eligr
Power BI Team

Re: How to Generate Embed Token in pure JavaScript

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.

Helpful resources

Announcements
Community News & Announcements

Community News & Announcements

Get your latest community news and announcements.

Summit North America

Power Platform Summit North America

Register by September 5 to save $200

Virtual Launch Event

Microsoft Business Applications Virtual Launch Event

Watch the event on demand for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

MBAS Gallery

Watch Sessions On Demand!

Continue your learning in our online communities.

Top Kudoed Authors
Users Online
Currently online: 152 members 2,229 guests
Please welcome our newest community members: