summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2012-03-21 22:58:36 -0700
committerAndrew Arnott <andrewarnott@gmail.com>2012-03-21 22:58:36 -0700
commit80028b1c5442c85909b889b3c52cfbd0c0121437 (patch)
treeb1b651c08a349957bb3d26ad5234a266d8d3e42e /src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs
parentf02ccf1e93367b7ab8bece3a2c53e960e98d221d (diff)
parente1455ee979b150d1ea4afdf1bc82a9e5cbc5b2ba (diff)
downloadDotNetOpenAuth-80028b1c5442c85909b889b3c52cfbd0c0121437.zip
DotNetOpenAuth-80028b1c5442c85909b889b3c52cfbd0c0121437.tar.gz
DotNetOpenAuth-80028b1c5442c85909b889b3c52cfbd0c0121437.tar.bz2
Merge branch 'v4.0' into dev11
Conflicts: src/DotNetOpenAuth.sln
Diffstat (limited to 'src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs')
-rw-r--r--src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs148
1 files changed, 148 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs
new file mode 100644
index 0000000..f4ad20b
--- /dev/null
+++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs
@@ -0,0 +1,148 @@
+//-----------------------------------------------------------------------
+// <copyright file="FacebookClient.cs" company="Microsoft">
+// Copyright (c) Microsoft. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.AspNet.Clients {
+ using System;
+ using System.Collections.Generic;
+ using System.Diagnostics.CodeAnalysis;
+ using System.Net;
+ using System.Web;
+ using DotNetOpenAuth.Messaging;
+
+ /// <summary>
+ /// The facebook client.
+ /// </summary>
+ [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Facebook", Justification = "Brand name")]
+ public sealed class FacebookClient : OAuth2Client {
+ #region Constants and Fields
+
+ /// <summary>
+ /// The authorization endpoint.
+ /// </summary>
+ private const string AuthorizationEndpoint = "https://www.facebook.com/dialog/oauth";
+
+ /// <summary>
+ /// The token endpoint.
+ /// </summary>
+ private const string TokenEndpoint = "https://graph.facebook.com/oauth/access_token";
+
+ /// <summary>
+ /// The _app id.
+ /// </summary>
+ private readonly string appId;
+
+ /// <summary>
+ /// The _app secret.
+ /// </summary>
+ private readonly string appSecret;
+
+ #endregion
+
+ #region Constructors and Destructors
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="FacebookClient"/> class.
+ /// </summary>
+ /// <param name="appId">
+ /// The app id.
+ /// </param>
+ /// <param name="appSecret">
+ /// The app secret.
+ /// </param>
+ public FacebookClient(string appId, string appSecret)
+ : base("facebook") {
+ Requires.NotNullOrEmpty(appId, "appId");
+ Requires.NotNullOrEmpty(appSecret, "appSecret");
+
+ this.appId = appId;
+ this.appSecret = appSecret;
+ }
+
+ #endregion
+
+ #region Methods
+
+ /// <summary>
+ /// The get service login url.
+ /// </summary>
+ /// <param name="returnUrl">
+ /// The return url.
+ /// </param>
+ /// <returns>An absolute URI.</returns>
+ 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.AppendQueryArgs(
+ new Dictionary<string, string> { { "client_id", this.appId }, { "redirect_uri", returnUrl.AbsoluteUri }, });
+ return builder.Uri;
+ }
+
+ /// <summary>
+ /// The get user data.
+ /// </summary>
+ /// <param name="accessToken">
+ /// The access token.
+ /// </param>
+ /// <returns>A dictionary of profile data.</returns>
+ protected override IDictionary<string, string> GetUserData(string accessToken) {
+ FacebookGraphData graphData;
+ var request =
+ WebRequest.Create(
+ "https://graph.facebook.com/me?access_token=" + MessagingUtilities.EscapeUriDataStringRfc3986(accessToken));
+ using (var response = request.GetResponse()) {
+ using (var responseStream = response.GetResponseStream()) {
+ graphData = JsonHelper.Deserialize<FacebookGraphData>(responseStream);
+ }
+ }
+
+ // this dictionary must contains
+ var userData = new Dictionary<string, string>();
+ userData.AddItemIfNotEmpty("id", graphData.Id);
+ userData.AddItemIfNotEmpty("username", graphData.Email);
+ userData.AddItemIfNotEmpty("name", graphData.Name);
+ userData.AddItemIfNotEmpty("link", graphData.Link == null ? null : graphData.Link.AbsoluteUri);
+ userData.AddItemIfNotEmpty("gender", graphData.Gender);
+ userData.AddItemIfNotEmpty("birthday", graphData.Birthday);
+ return userData;
+ }
+
+ /// <summary>
+ /// Obtains an access token given an authorization code and callback URL.
+ /// </summary>
+ /// <param name="returnUrl">
+ /// The return url.
+ /// </param>
+ /// <param name="authorizationCode">
+ /// The authorization code.
+ /// </param>
+ /// <returns>
+ /// The access token.
+ /// </returns>
+ 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.AppendQueryArgs(
+ new Dictionary<string, string> {
+ { "client_id", this.appId },
+ { "redirect_uri", returnUrl.AbsoluteUri },
+ { "client_secret", this.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);
+ return parsedQueryString["access_token"];
+ }
+ }
+
+ #endregion
+ }
+}