diff options
Diffstat (limited to 'src/DotNetOpenAuth.Web/Clients/OAuth2/FacebookClient.cs')
-rw-r--r-- | src/DotNetOpenAuth.Web/Clients/OAuth2/FacebookClient.cs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Web/Clients/OAuth2/FacebookClient.cs b/src/DotNetOpenAuth.Web/Clients/OAuth2/FacebookClient.cs new file mode 100644 index 0000000..e9e0b80 --- /dev/null +++ b/src/DotNetOpenAuth.Web/Clients/OAuth2/FacebookClient.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Net; +using System.Web; +using DotNetOpenAuth.Web.Resources; + +namespace DotNetOpenAuth.Web.Clients +{ + internal sealed class FacebookClient : OAuth2Client + { + private const string AuthorizationEndpoint = "https://www.facebook.com/dialog/oauth"; + private const string TokenEndpoint = "https://graph.facebook.com/oauth/access_token"; + + private readonly string _appId; + private readonly string _appSecret; + + public FacebookClient(string appId, string appSecret) + : base("facebook") + { + if (String.IsNullOrEmpty(appId)) + { + throw new ArgumentException( + String.Format(CultureInfo.CurrentCulture, WebResources.Argument_Cannot_Be_Null_Or_Empty, "appId"), + "appId"); + } + + if (String.IsNullOrEmpty("appSecret")) + { + throw new ArgumentException( + String.Format(CultureInfo.CurrentCulture, WebResources.Argument_Cannot_Be_Null_Or_Empty, "appSecret"), + "appSecret"); + } + + _appId = appId; + _appSecret = appSecret; + } + + protected override Uri GetServiceLoginUrl(Uri returnUrl) + { + // Note: Facebook doesn't like us to url-encode the redirect_uri value + var builder = new UriBuilder(AuthorizationEndpoint); + builder.AppendQueryArguments(new Dictionary<string, string> + { + { "client_id", _appId }, + { "redirect_uri", returnUrl.ToString() } + }); + return builder.Uri; + } + + protected override string QueryAccessToken(Uri returnUrl, string authorizationCode) + { + // Note: Facebook doesn't like us to url-encode the redirect_uri value + var builder = new UriBuilder(TokenEndpoint); + builder.AppendQueryArguments(new Dictionary<string, string> + { + { "client_id", _appId }, + { "redirect_uri", returnUrl.ToString() }, + { "client_secret", _appSecret }, + { "code", authorizationCode } + }); + + using (WebClient client = new WebClient()) + { + string data = client.DownloadString(builder.Uri); + if (String.IsNullOrEmpty(data)) + { + return null; + } + + var parsedQueryString = HttpUtility.ParseQueryString(data); + if (parsedQueryString != null) + { + return parsedQueryString["access_token"]; + } + } + return null; + } + + protected override IDictionary<string, string> GetUserData(string accessToken) + { + FacebookGraph graph; + var request = WebRequest.Create("https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(accessToken)); + using (var response = request.GetResponse()) + { + using (var responseStream = response.GetResponseStream()) + { + graph = JsonHelper.Deserialize<FacebookGraph>(responseStream); + } + } + + // this dictionary must contains + var userData = new Dictionary<string, string>(); + userData.AddItemIfNotEmpty("id", graph.Id); + userData.AddItemIfNotEmpty("username", graph.Email); + userData.AddItemIfNotEmpty("name", graph.Name); + userData.AddItemIfNotEmpty("link", graph.Link == null ? null : graph.Link.ToString()); + userData.AddItemIfNotEmpty("gender", graph.Gender); + userData.AddItemIfNotEmpty("birthday", graph.Birthday); + return userData; + } + } +} |