public void CanCreateReportEmbedTokenForDatasetId()
        {
            var workspaceId = Guid.NewGuid().ToString();
            var datasetId   = Guid.NewGuid().ToString();

            var token = PowerBIToken.CreateReportEmbedTokenForCreation("Contoso", workspaceId, datasetId: datasetId, username: "******", roles: new[] { "TestRole" });

            Assert.IsNotNull(token);
            var jwt = token.Generate(this.accessKey);

            Assert.IsFalse(string.IsNullOrEmpty(jwt));

            var decodedToken = new JwtSecurityToken(jwt);

            var versionClaim  = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.Version);
            var typeClaim     = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.JwtType);
            var wcnClaim      = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.WorkspaceCollectionName);
            var widClaim      = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.WorkspaceId);
            var didCliam      = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.DatasetId);
            var usernameClaim = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.Username);
            var rolesClaim    = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.Roles);

            Assert.AreEqual("PowerBISDK", decodedToken.Issuer);
            Assert.IsTrue(decodedToken.Audiences.Contains("https://analysis.windows.net/powerbi/api"));
            Assert.IsTrue(decodedToken.ValidTo >= DateTime.UtcNow);
            Assert.IsTrue(decodedToken.ValidTo <= DateTime.UtcNow.AddHours(1));
            Assert.AreEqual("0.2.0", versionClaim.Value);
            Assert.AreEqual("embed", typeClaim.Value);
            Assert.AreEqual("Contoso", wcnClaim.Value);
            Assert.AreEqual(workspaceId, widClaim.Value);
            Assert.AreEqual(datasetId, didCliam.Value);
            Assert.AreEqual("TestUser", usernameClaim.Value);
            Assert.AreEqual("TestRole", rolesClaim.Value);
        }
        static async Task GetEmbedInfo()
        {
            EnsureBasicParams(EnsureExtras.WorkspaceCollection | EnsureExtras.WorspaceId);
            int?index = 1;

            Console.WriteLine("Select Embed Mode:");
            Console.WriteLine("1) View Mode:            Report.Read");
            Console.WriteLine("2) Edit & Save Mode:     Report.ReadWrite");
            Console.WriteLine("3) View & Save As Mode:  Report.ReadWrite Workspace.Report.Create");
            Console.WriteLine("4) Edit & Save As Mode:  Report.Read Workspace.Report.Create");
            Console.WriteLine("5) Create Report Mode:   Dataset.Read Workspace.Report.Create");

            int?mode = userInput.EnsureIntParam((int)EmbedMode.View, "Embed mode");

            if (!mode.HasValue || mode.Value <= 0 || mode.Value > 5)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("selected mode is out of range.");
                return;
            }

            var    embedMode = (EmbedMode)mode.Value;
            string reportId  = null;
            string datasetId = null;
            string embedUrl  = null;

            if (embedMode == EmbedMode.View || embedMode == EmbedMode.ViewAndSaveAs || embedMode == EmbedMode.EditAndSaveAs || embedMode == EmbedMode.EditAndSave)
            {
                // For these modes user need to select a report to embed
                var reports = await GetReports(workspaceCollectionName, workspaceId);

                if (!PrintReports(reports))
                {
                    return;
                }

                index = userInput.EnsureIntParam(index, "Index of report to use");
                if (!index.HasValue || index.Value <= 0 || index.Value > reports.Count)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Report index is out of range.");
                    return;
                }

                var report = reports[index.Value - 1];
                reportId = report.Id;
                embedUrl = report.EmbedUrl;
            }
            else
            {
                // For these modes user need to select a dataset to create a report with
                var datasets = await GetDatasets(workspaceCollectionName, workspaceId);

                PrintDatasets(datasets);

                index = userInput.EnsureIntParam(index, "Index of dataset to create a report with");
                if (!index.HasValue || index.Value <= 0 || index.Value > datasets.Count)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Dataset index is out of range.");
                    return;
                }

                var dataset = datasets[index.Value - 1];
                datasetId = dataset.Id;
                embedUrl  = defaultEmbedUrl;
            }

            var scopes = string.Empty;

            switch (embedMode)
            {
            case EmbedMode.View:
                scopes = "Report.Read";
                break;

            case EmbedMode.EditAndSave:
                scopes = "Report.ReadWrite";
                break;

            case EmbedMode.ViewAndSaveAs:
                scopes = "Report.Read Workspace.Report.Create";
                break;

            case EmbedMode.EditAndSaveAs:
                scopes = "Report.ReadWrite Workspace.Report.Create";
                break;

            case EmbedMode.CreateMode:
                scopes = "Dataset.Read Workspace.Report.Create";
                break;

            default:
                scopes = string.Empty;
                break;
            }

            // RLS
            var rlsUsername = userInput.EnsureParam(null, "RLS - limit to specific user: (Keep empty to create without RLS)");
            var rlsRoles    = userInput.EnsureParam(null, "RLS - limit to specific roles: (comma separated)");
            var roles       = string.IsNullOrEmpty(rlsRoles) ? null : rlsRoles.Split(',');

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Embed Url: {0}", embedUrl);

            TimeSpan expiry = new TimeSpan(24, 0, 0);

            PowerBIToken embedToken = null;

            if (!string.IsNullOrEmpty(reportId))
            {
                embedToken = PowerBIToken.CreateReportEmbedToken(workspaceCollectionName, workspaceId, reportId, expiry, rlsUsername, roles, scopes);
            }
            else if (!string.IsNullOrEmpty(datasetId))
            {
                embedToken = PowerBIToken.CreateReportEmbedTokenForCreation(workspaceCollectionName, workspaceId, datasetId, expiry, rlsUsername, roles, scopes);
            }

            var token = embedToken.Generate(accessKeys.Key1);

            Console.WriteLine("Embed Token: {0}", token);

            var copy = userInput.EnsureParam(null, "Copy embed token to clipboard? (Y)/(N) ");

            if (copy.Equals("y", StringComparison.CurrentCultureIgnoreCase))
            {
                try
                {
                    // Save access token to Clipboard
                    System.Windows.Forms.Clipboard.SetText(token);

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Embed Token saved to clipboard.");
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Embed Token could not be saved to clipboard.");
                    Console.WriteLine(ex.ToString());
                }
            }
        }