Skip to content
Einar Ingebrigtsen edited this page Oct 26, 2015 · 1 revision

Getting Started

The library has a few ways it can be used. Firstly it represents a wrapper for the REST API and makes it simpler to work with. Secondly, it holds helpers to get started with multi-tenancy.

The Wrapper

Getting started with the wrapper part is very simple. All you need is an authorization token. The Authentication.cs represents an implementation of the authentication code needed and can also be used directly for getting the tokens.

If you're authenticating from a desktop application, you can add the following code for getting the tokens:

var authentication = new Authentication();
var tokens = authentication.GetTokens("client id");

For this to work, you either needs to have your application registered as a desktop application or you need add a Reply URL that the desktop authentication is using: http://login.live.com/oauth20_desktop.srf.

If you are authenticating from a Web app, you need it to be a bit different. Your application needs to redirect to the authority, in this case the login.windows.net authority. When redirecting to it, we're telling it at the same time where to reply to - the same place we configured in Azure Active Directory.

var tenant = "{name of tenant}";
var redirectUri = "http://localhost:30348/Redirect.ashx";
var authorityUri = "https://login.windows.net/common/oauth2/authorize/";

var @params = new NameValueCollection
{
	{"response_type", "code"},
	{"client_id", client},
	{"resource", "https://analysis.windows.net/powerbi/api"},
	{"redirect_uri", redirectUri},
	{"state", tenant}
};

//Create sign-in query string
var queryString = HttpUtility.ParseQueryString(string.Empty);
        queryString.Add(@params);

var redirectUrl = $"{authorityUri}?{queryString}";
HttpContext.Current.Response.Redirect(redirectUrl);

Make note of the "state" parameter being passed with the tenant information. This is useful if you need to know coming back to the reply URL which tenant we were trying to authenticate. There are other ways to keep this information by utilizing claims based authentication and getting the TenantId claim from the Azure Active Directory. The code above shows how we can redirect back to a generic HTTP handler (ASHX). This can of course be anything that can accept the post coming back.

In the code that handles the reply, you will need to do the following:

var redirectUri = "http://localhost:30348/Redirect.ashx";

// In an IHttpHandler implementation, the ProcessRequest() method takes a HttpContext. 
var token = context.Request.Params["code"];
var tenant = context.Request.Params["state"];

var authentication = new Authentication();
var tokens = authentication.GetTokens("client id", "client secret", token, redirectUri);

In the tokens object returned you get the AccessToken and the RefreshToken. The AccessToken is the one that we can then be using in the rest of the API. The RefreshToken is something we want to use to be keeping the application and its tokens alive. Typically we would refresh the token before we call off to Power BI.

Now that we have a token, we can use this for the wrapper API:

var workspace = Workspace.GetFor("access token");

Inside the Workspace we have all the capabilities exposed. We can now start using the Dataset, Tables, Rows, Groups, Dashboards and Imports.