Forum Discussion

Zirochka's avatar
Zirochka
Frequent Visitor
9 years ago
Solved

Generate Embed Token For Create Issue

Hello. I have an issue with getting embed token. I've done what's described in this link (https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-walkthrough-push-data/) and everything w...
  • Eric_Zhang's avatar
    9 years ago

    Zirochka

    I may doubt that there's some issue with the Power BI API lib, as I've found the token generation functions for reports, dashboards, tiles but not for datasets.

     

    powerBIclient.Reports.GenerateTokenxxxxx
    powerBIclient.Dashboards.GenerateTokenxxxxx
    powerBIclient.Tiles.GenerateTokenxxxxx

    So I guess for Datasets, it should be
    powerBIclient.Datasets.GenerateTokenxxxxx
    Unfortunately, I'm not finding any GenerateTokenxxxxx for datasets

     

    Instead of using the API lib, call the REST API directly.

    using System;
    //Install-Package Newtonsoft.Json 
    using Newtonsoft.Json;
    using System.Net;
    using System.IO;
    
    namespace GenerateEmbeddedToken
    {
        class Program
        {
            static void Main(string[] args)
            {
                string EmbedTokenForDataSet = generateEmbedTokenForDataSet("the groupID", "the datasetID", "eyxxxxxxxxxtoken");
    
                Console.WriteLine(EmbedTokenForDataSet);
                Console.ReadKey();
            }
    
    
            private static string generateEmbedTokenForDataSet(string groupID, string datasetID, string accessBearerToken)
            {
                string embedToken = "";
                HttpWebRequest request = System.Net.HttpWebRequest.CreateHttp(String.Format("https://api.powerbi.com/v1.0/myorg/groups/{0}/reports/GenerateToken", groupID));
                //POST web request to create a datasource.
                request.KeepAlive = true;
                request.Method = "POST";
                request.ContentType = "application/json";
                 
                //Add token to the request header
                request.Headers.Add("Authorization", String.Format("Bearer {0}", accessBearerToken));
    
                string PostData = "{"+
                                          "\"accessLevel\": \"Create\","+
                                          "\"datasetId\": \""+datasetID+"\","+
                                          "\"allowSaveAs\": true"+
                                        "}";
    
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(PostData); 
    
                try
                {
                    //Write JSON byte[] into a Stream
                    using (Stream writer = request.GetRequestStream())
                    {
                        writer.Write(byteArray, 0, byteArray.Length);
                        var response = (HttpWebResponse)request.GetResponse(); 
                        var responseJson = new StreamReader(response.GetResponseStream()).ReadToEnd();
                        dynamic responseString = JsonConvert.DeserializeObject<dynamic>(responseJson.ToString());
                        embedToken = responseString["token"];
                    }
                }
                catch (WebException wex)
                {
                    if (wex.Response != null)
                    {
                        using (var errorResponse = (HttpWebResponse)wex.Response)
                        {
                            using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                            {
                                string errorString = reader.ReadToEnd();
                                dynamic respJson = JsonConvert.DeserializeObject<dynamic>(errorString);
                                Console.WriteLine(respJson.toString());
                                //TODO: use JSON.net to parse this string and look at the error message
                            }
                        }
                    }
                }
    
                return embedToken;
            }
        }
    }