<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Generate Embed Token For Create Issue in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/222047#M7013</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/17074"&gt;@Zirochka&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I tried to use your&amp;nbsp;function, but it didn't help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;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&amp;lt;dynamic&amp;gt;(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&amp;lt;dynamic&amp;gt;(errorString);
                            Console.WriteLine(respJson.toString());
                            //TODO: use JSON.net to parse this string and look at the error message
                        }
                    }
                }
            }

            return embedToken;
        }
    }
}&lt;/PRE&gt;
&lt;P&gt;On line:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;using (Stream writer = request.GetRequestStream())&lt;/PRE&gt;
&lt;P&gt;program thinks for&amp;nbsp;&lt;SPAN&gt;approximately 30 seconds and then goes to catch block. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;In catch block if statement is false (so, Response == null)&amp;nbsp;and program just goes out from catch.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;That's all.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I still don't understant why it happens because I just use this guide for POST requests (&lt;A title="Generate token" href="https://msdn.microsoft.com/library/mt784614.aspx" target="_blank"&gt;https://msdn.microsoft.com/library/mt784614.aspx&lt;/A&gt;). And it's up to date (Updated: July 21, 2017), so, I don't understand.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I'll appresiate any help.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/17074"&gt;@Zirochka&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Have you got any API console tool for testing purpose? If not, I'd suggest using &lt;A href="https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en" target="_self"&gt;POSTMAN&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;So beforing coding in C#, can you test this&amp;nbsp;GenerateToken in postman? For any issue, feel free to post.&lt;/P&gt;</description>
    <pubDate>Mon, 31 Jul 2017 09:36:07 GMT</pubDate>
    <dc:creator>Eric_Zhang</dc:creator>
    <dc:date>2017-07-31T09:36:07Z</dc:date>
    <item>
      <title>Generate Embed Token For Create Issue</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/219683#M6922</link>
      <description>&lt;DIV&gt;&lt;SPAN&gt;Hello.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I have an issue with getting embed token. I've done what's described in this link (&lt;A href="https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-walkthrough-push-data/" target="_blank"&gt;https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-walkthrough-push-data/&lt;/A&gt;) and everything works perfectly: datasets creates, tables creates and fills with data without any problems.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I have a task to embed a datasets (not reports) to web page and here I'm getting an error.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I found this page (&lt;A href="https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html" target="_blank"&gt;https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html&lt;/A&gt;) which shows how embed report/dataset/dashboard/tile will look like.&amp;nbsp;And on this page there is an embed token.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I googled a little and found this page (&lt;A href="https://msdn.microsoft.com/library/mt784614.aspx" target="_blank"&gt;https://msdn.microsoft.com/library/mt784614.aspx&lt;/A&gt;) which describes the looks oh HTTP POST requests. I did the part for datasets. Here is my code example:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV align="left"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV align="left"&gt;&lt;DIV&gt;&lt;PRE&gt;private static void generateEmbedToken()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // TokenCredentials Initializes a new instance of the
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Microsoft.Rest.TokenCredentials class with
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // the given 'Bearer' token.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var credentials = new TokenCredentials(token);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Initialize PowerBIClient with credentials
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var powerBIclient = new Microsoft.PowerBI.Api.V2.PowerBIClient(credentials)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // BaseUri is the api endpoint, default is https://api.powerbi.com
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BaseUri = new Uri("https://api.powerbi.com")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Create body where accessLevel = View, datasetId = "" by default
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var requestParameters = new GenerateTokenRequest(TokenAccessLevel.Create, datasetId, true);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Generate EmbedToken This function sends the POST message
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //with all parameters and returns the token
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EmbedToken token = powerBIclient.Reports.GenerateTokenForCreate(requestParameters);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; embedToken = token.Token;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch (Exception exc)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(exc.ToString());
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;and I got next issue:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;Microsoft.Rest.HttpOperationException: Operation returned an invalid status code 'NotFound'&lt;BR /&gt; at Microsoft.PowerBI.Api.V2.Reports.&amp;lt;GenerateTokenForCreateWithHttpMessagesAsync&amp;gt;d__8.MoveNext()&lt;BR /&gt;--- End of stack trace from previous location where exception was thrown ---&lt;BR /&gt; at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)&lt;BR /&gt; at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)&lt;BR /&gt; at Microsoft.PowerBI.Api.V2.ReportsExtensions.&amp;lt;GenerateTokenForCreateAsync&amp;gt;d__7.MoveNext()&lt;BR /&gt;--- End of stack trace from previous location where exception was thrown ---&lt;BR /&gt; at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)&lt;BR /&gt; at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)&lt;BR /&gt; at Microsoft.PowerBI.Api.V2.ReportsExtensions.GenerateTokenForCreate(IReports operations, GenerateTokenRequest requestParameters)&lt;BR /&gt; at PushDataApp.Program.generateEmbedToken() in C:\Users\PC\Documents\Visual Studio 2017\Projects\PushDataApp\PushDataApp\Program.cs:line 388&lt;/PRE&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;line 388 here:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;EmbedToken token = powerBIclient.Reports.GenerateTokenForCreate(requestParameters);&lt;/PRE&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I don't know why it's happening.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I got this code from here (&lt;A href="https://msdn.microsoft.com/library/mt784614.aspx" target="_blank"&gt;https://msdn.microsoft.com/library/mt784614.aspx&lt;/A&gt;), but I did some changes for my purposes (because I need dataset and not report).&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Consolas" size="2"&gt;I'll&amp;nbsp;&lt;SPAN&gt;appreciate&lt;/SPAN&gt;&amp;nbsp;any help.&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2017 10:12:11 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/219683#M6922</guid>
      <dc:creator>Zirochka</dc:creator>
      <dc:date>2017-07-27T10:12:11Z</dc:date>
    </item>
    <item>
      <title>Re: Generate Embed Token For Create Issue</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/220870#M6972</link>
      <description>&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/17074"&gt;@Zirochka&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;powerBIclient.&lt;STRONG&gt;Reports&lt;/STRONG&gt;.GenerateTokenxxxxx
powerBIclient.&lt;STRONG&gt;Dashboards&lt;/STRONG&gt;.GenerateTokenxxxxx
powerBIclient.&lt;STRONG&gt;Tiles&lt;/STRONG&gt;.GenerateTokenxxxxx&lt;BR /&gt;&lt;BR /&gt;So I guess for Datasets, it should be &lt;BR /&gt;powerBIclient.&lt;STRONG&gt;Datasets&lt;/STRONG&gt;.GenerateTokenxxxxx&lt;BR /&gt;Unfortunately, I'm not finding any GenerateTokenxxxxx for datasets&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of using the API lib, call the REST API directly.&lt;/P&gt;
&lt;PRE&gt;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&amp;lt;dynamic&amp;gt;(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&amp;lt;dynamic&amp;gt;(errorString);
                            Console.WriteLine(respJson.toString());
                            //TODO: use JSON.net to parse this string and look at the error message
                        }
                    }
                }
            }

            return embedToken;
        }
    }
}
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2017 08:04:00 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/220870#M6972</guid>
      <dc:creator>Eric_Zhang</dc:creator>
      <dc:date>2017-07-28T08:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: Generate Embed Token For Create Issue</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/221958#M7004</link>
      <description>&lt;P&gt;I tried to use your&amp;nbsp;function, but it didn't help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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&amp;lt;dynamic&amp;gt;(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&amp;lt;dynamic&amp;gt;(errorString);
                            Console.WriteLine(respJson.toString());
                            //TODO: use JSON.net to parse this string and look at the error message
                        }
                    }
                }
            }

            return embedToken;
        }
    }
}&lt;/PRE&gt;&lt;P&gt;On line:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;using (Stream writer = request.GetRequestStream())&lt;/PRE&gt;&lt;P&gt;program thinks for&amp;nbsp;&lt;SPAN&gt;approximately 30 seconds and then goes to catch block. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In catch block if statement is false (so, Response == null)&amp;nbsp;and program just goes out from catch.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;That's all.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I still don't understant why it happens because I just use this guide for POST requests (&lt;A title="Generate token" href="https://msdn.microsoft.com/library/mt784614.aspx" target="_blank"&gt;https://msdn.microsoft.com/library/mt784614.aspx&lt;/A&gt;). And it's up to date (Updated: July 21, 2017), so, I don't understand.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I'll appresiate any help.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jul 2017 08:19:58 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/221958#M7004</guid>
      <dc:creator>Zirochka</dc:creator>
      <dc:date>2017-07-31T08:19:58Z</dc:date>
    </item>
    <item>
      <title>Re: Generate Embed Token For Create Issue</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/222047#M7013</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/17074"&gt;@Zirochka&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I tried to use your&amp;nbsp;function, but it didn't help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;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&amp;lt;dynamic&amp;gt;(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&amp;lt;dynamic&amp;gt;(errorString);
                            Console.WriteLine(respJson.toString());
                            //TODO: use JSON.net to parse this string and look at the error message
                        }
                    }
                }
            }

            return embedToken;
        }
    }
}&lt;/PRE&gt;
&lt;P&gt;On line:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;using (Stream writer = request.GetRequestStream())&lt;/PRE&gt;
&lt;P&gt;program thinks for&amp;nbsp;&lt;SPAN&gt;approximately 30 seconds and then goes to catch block. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;In catch block if statement is false (so, Response == null)&amp;nbsp;and program just goes out from catch.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;That's all.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I still don't understant why it happens because I just use this guide for POST requests (&lt;A title="Generate token" href="https://msdn.microsoft.com/library/mt784614.aspx" target="_blank"&gt;https://msdn.microsoft.com/library/mt784614.aspx&lt;/A&gt;). And it's up to date (Updated: July 21, 2017), so, I don't understand.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I'll appresiate any help.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/17074"&gt;@Zirochka&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Have you got any API console tool for testing purpose? If not, I'd suggest using &lt;A href="https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en" target="_self"&gt;POSTMAN&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;So beforing coding in C#, can you test this&amp;nbsp;GenerateToken in postman? For any issue, feel free to post.&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jul 2017 09:36:07 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/222047#M7013</guid>
      <dc:creator>Eric_Zhang</dc:creator>
      <dc:date>2017-07-31T09:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: Generate Embed Token For Create Issue</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/222095#M7016</link>
      <description>&lt;P&gt;Thanks for this Postman. It's really useful tool. Thanks a lot.&lt;/P&gt;&lt;P&gt;I tried to sent in this postman post request (from here&amp;nbsp;&lt;A href="https://msdn.microsoft.com/library/mt784614.aspx" target="_blank"&gt;https://msdn.microsoft.com/library/mt784614.aspx&lt;/A&gt;&amp;nbsp;for dataset) and I got an answer with embed token. I checked this token here (&lt;A href="https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html" target="_blank"&gt;https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html&lt;/A&gt;) and it works. Dataset loads to this example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I don't understand why from C# it doesn't works.&lt;/P&gt;&lt;P&gt;In my code I have another POST requests: when I create dataset (first POST) and when I push data to the dataset (second POST). And they works perfectly (I checked hundred times). But this (when I get embed token) POST (it's third POST in my code) does't work. And I found out that API works fine (thanks to postman).&lt;/P&gt;&lt;P&gt;Do you have any idea?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll&amp;nbsp;appreciate any help.&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jul 2017 10:55:43 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/222095#M7016</guid>
      <dc:creator>Zirochka</dc:creator>
      <dc:date>2017-07-31T10:55:43Z</dc:date>
    </item>
    <item>
      <title>Re: Generate Embed Token For Create Issue</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/225659#M7130</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/17074"&gt;@Zirochka&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for this Postman. It's really useful tool. Thanks a lot.&lt;/P&gt;
&lt;P&gt;I tried to sent in this postman post request (from here&amp;nbsp;&lt;A href="https://msdn.microsoft.com/library/mt784614.aspx" target="_blank"&gt;https://msdn.microsoft.com/library/mt784614.aspx&lt;/A&gt;&amp;nbsp;for dataset) and I got an answer with embed token. I checked this token here (&lt;A href="https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html" target="_blank"&gt;https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html&lt;/A&gt;) and it works. Dataset loads to this example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;But I don't understand why from C# it doesn't works.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;In my code I have another POST requests: when I create dataset (first POST) and when I push data to the dataset (second POST). And they works perfectly (I checked hundred times). But this (when I get embed token) POST&lt;STRONG&gt; (it's third POST in my code) does't work&lt;/STRONG&gt;. And I found out that API works fine (thanks to postman).&lt;/P&gt;
&lt;P&gt;Do you have any idea?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll&amp;nbsp;appreciate any help.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/17074"&gt;@Zirochka&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;I'd say the C# code in my first indeed works as long as the groupid and datesetid are correct. In your case, as the generate embedtoken POST request works in POSTMAN, so I think you could &lt;STRONG&gt;debug&lt;/STRONG&gt; your code and figure it out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to your "another POST requests", this is one issue as I tested in this &lt;A href="https://community.powerbi.com/t5/Developer/All-API-calls-failing-after-the-3rd-consecutive-call/m-p/223245" target="_self"&gt;thread&lt;/A&gt;&amp;nbsp;and you can use the workaround from orginal poster.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Aug 2017 01:57:09 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Generate-Embed-Token-For-Create-Issue/m-p/225659#M7130</guid>
      <dc:creator>Eric_Zhang</dc:creator>
      <dc:date>2017-08-10T01:57:09Z</dc:date>
    </item>
  </channel>
</rss>

