OAuth on Windows 8

tapanila.netThis is a cross-post from Teemu’s blog.

OAuth is open authentication standard that can be used to give an access to the specific resources of the account. Instead of giving full access in form of username and password to 3rd party applications. Microsoft added a option to use OAuth in WinRT.

Here’s an example how to use it to authenticate to Facebook.

private async void Authenticate()
{
//Facebook Authentication Uri
var facebookUri = "https://www.facebook.com/dialog/oauth";
//Standard redirect uri for desktop/non-web based apps
var redirectUri = "https://www.facebook.com/connect/login_success.html";
//Place your appclient id here
var clientId = "";
//The type of token that can be requested
var responseType = "token";
//Response pattern
var pattern = string.Format("{0}#access_token={1}&expires_in={2}", redirectUri, "(?.+)", "(?.+)");
//Access scope wanted
var scope = "read_stream";
try
{
String FacebookURL = "https://www.facebook.com/dialog/oauth?client_id=" +
clientId + "&redirect_uri=" + Uri.EscapeUriString(redirectUri) + "&scope=" + scope + "&display=touch&response_type=" + responseType;

System.Uri StartUri = new Uri(FacebookURL);
System.Uri EndUri = new Uri(redirectUri);

WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
StartUri,
EndUri);
if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
{
//Authenticated succesfully
var response = WebAuthenticationResult.ResponseData.ToString();
}
else if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.ErrorHttp)
{
//Handle HTTP error
}
else
{
//Authentication failed
}
}
catch (Exception ex)
{
//Handle error
}
}

About the author
TeemuTapanilaTeemu Tapanila
Teemu is a computer science student at the Aalto University who aspires to become a professional developer. He is involved in the Windows Phone Aalto Community. Currently Teemu is working as a certified guru in AppCampus.

Advertisements

About arcticmsp

The Finnish Microsoft Student Partners are a group of enthusiastic students, who are interested in technology and all things new, techy and cool!
This entry was posted in Development, Windows 8. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s