<commit_before>Skybrud.Social.Microsoft
========================

As this is an experimental project, it won't be released on NuGet or similar for now. You can however [**grab a build from Dropbox**](https://www.dropbox.com/sh/ubak1qionvji8mf/AACY2DjhldUXVhTpc3X8b8oXa/Skybrud.Social.Microsoft%20-%20Build%200.0.1.2%20%282015-09-12%29?dl=0) or download the repository and build the `Skybrud.Social.Microsoft` project.

## Usage

**Initializing a new OAuth client**<br />

The `MicrosoftOAuthClient` class is responsible for the raw communication with the various Microsoft APIs as well as authentication with Windows Live. The class can be initialized with one of the constructors, or simply by setting the properties like in the examples below:

```C#
// Initialize and configure the OAuth client
MicrosoftOAuthClient client = new MicrosoftOAuthClient {
    AccessToken = "Insert your access token here"
};
```

or:

```C#
// Initialize and configure the OAuth client
MicrosoftOAuthClient client = new MicrosoftOAuthClient {
    ClientId = "Insert your client ID here",
    ClientSecret = "Insert your client secret here",
    RedirectUri = "http://social.abjerner/microsoft/oauth/"
};
```

Authentication requires that you specify the client ID, client secret and redirect URI of your app (client).

* [**Create a new app** *at account.live.com*](https://account.live.com/developers/applications/create)
* [**List of existing app** *at account.live.com*](https://account.live.com/developers/applications/index)

<br />
<br />
**Generating the authorization URL / getting an authorization code**<br />

To start authenticating the user, you should generate and redirect the user to the authorization URL. The authorization URL is constructed of the client ID, client secret and redirect URI of the OAuth client as well as a state (random value for security purposes) and the scope (permissions) which your user should grant your app:

```C#
// Construct the authorization URL
string url = client.GetAuthorizationUrl(state, WindowsLiveScopes.Emails + WindowsLiveScopes.Birthday);
```

When redirecting the user to the auhtorization URL, the user will be given the option to grant your app access to the specified scope. After a successful login, the user is redirected back to the specified redirect URI, where the `state` and authorization `code` parameters are specified in the query string.

<br />
<br />
**Obtaining an access token**<br />

The authorization code can be obtained using the `GetAccessTokenFromAuthCode` method, where the response body will reveil the access token as well as some other information - eg. like the amount of seconds to when the access token will expire, or the refresh token if the `WindowsLiveScopes.OfflineAccess` scope has been specified.

```C#
// Exchange the authorization code for an access token
MicrosoftTokenResponse response = client.GetAccessTokenFromAuthCode(input.Code);

// Get the access token from the response body
string accessToken = response.Body.AccessToken;
```

<br />
<br />
**Initializing an instance of MicrosoftService**<br />

The `MicrosoftService` class can be initialized in a few different ways. Eg. from an access token:

```C#
// Initialize a new service instance from an access token
MicrosoftService service = MicrosoftService.CreateFromAccessToken("Insert your access token here");
```

Or from a refresh token:<commit_msg>Microsoft: Updated the readme a bit<commit_after>Skybrud.Social.Microsoft
========================

As this is an experimental project, it won't be released on NuGet or similar for now. You can however [**grab a build from Dropbox**](https://www.dropbox.com/sh/ubak1qionvji8mf/AACY2DjhldUXVhTpc3X8b8oXa/Skybrud.Social.Microsoft%20-%20Build%200.0.1.2%20%282015-09-12%29?dl=0) or download the repository and build the `Skybrud.Social.Microsoft` project.

## Usage

**Initializing a new OAuth client**<br />

The `MicrosoftOAuthClient` class is responsible for the raw communication with the various Microsoft APIs as well as authentication with Windows Live. The class can be initialized with one of the constructors, or simply by setting the properties like in the examples below:

```C#
// Initialize and configure the OAuth client
MicrosoftOAuthClient client = new MicrosoftOAuthClient {
    AccessToken = "Insert your access token here"
};
```

or:

```C#
// Initialize and configure the OAuth client
MicrosoftOAuthClient client = new MicrosoftOAuthClient {
    ClientId = "Insert your client ID here",
    ClientSecret = "Insert your client secret here",
    RedirectUri = "http://social.abjerner/microsoft/oauth/"
};
```

Authentication requires that you specify the client ID, client secret and redirect URI of your app (client).

* [**Create a new app** *at account.live.com*](https://account.live.com/developers/applications/create)
* [**List of existing app** *at account.live.com*](https://account.live.com/developers/applications/index)

<br />
<br />
**Generating the authorization URL / getting an authorization code**<br />

To start authenticating the user, you should generate and redirect the user to the authorization URL. The authorization URL is constructed of the client ID, client secret and redirect URI of the OAuth client as well as a state (random value for security purposes) and the scope (permissions) which your user should grant your app:

```C#
// Construct the authorization URL
string url = client.GetAuthorizationUrl(state, WindowsLiveScopes.Emails + WindowsLiveScopes.Birthday);
```

When redirecting the user to the auhtorization URL, the user will be given the option to grant your app access to the specified scope. After a successful login, the user is redirected back to the specified redirect URI, where the `state` and authorization `code` parameters are specified in the query string.

<br />
<br />
**Obtaining an access token**<br />

The authorization code can be obtained using the `GetAccessTokenFromAuthCode` method, where the response body will reveil the access token as well as some other information - eg. like the amount of seconds to when the access token will expire, or the refresh token if the `WindowsLiveScopes.OfflineAccess` scope has been specified.

```C#
// Exchange the authorization code for an access token
MicrosoftTokenResponse response = client.GetAccessTokenFromAuthCode(Request.QueryString["code"]);

// Get the access token from the response body
string accessToken = response.Body.AccessToken;
```

<br />
<br />
**Initializing an instance of MicrosoftService**<br />

The `MicrosoftService` class can be initialized in a few different ways. Eg. from an access token:

```C#
// Initialize a new service instance from an access token
MicrosoftService service = MicrosoftService.CreateFromAccessToken("Insert your access token here");
```

Or from a refresh token:
{'fraction_non_alphanumeric': 0.09029708766281282, 'fraction_numerical': 0.009073613346992537, 'mean_word_length': 4.973776223776224, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4137686', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1763, 'n_words': 810}