summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.AspNet/Clients/OAuth2/FacebookClient.cs
blob: 442326ecdb292e4cc8146cd63ca8f9fc73725499 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//-----------------------------------------------------------------------
// <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.Globalization;
	using System.Net;
	using System.Web;
	using DotNetOpenAuth.AspNet.Resources;
	using DotNetOpenAuth.Messaging;

	public 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);
			MessagingUtilities.AppendQueryArgs(builder,
				new KeyValuePair<string, string>[] {
                    new KeyValuePair<string, string>("client_id", _appId),
                    new KeyValuePair<string, string>("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);
			MessagingUtilities.AppendQueryArgs(builder,
				new KeyValuePair<string, string>[] {
                    new KeyValuePair<string, string>("client_id", _appId),
                    new KeyValuePair<string, string>("redirect_uri", returnUrl.ToString()),
                    new KeyValuePair<string, string>("client_secret", _appSecret),
                    new KeyValuePair<string, string>("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) {
			FacebookGraphData graphData;
			var request = WebRequest.Create("https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(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.ToString());
			userData.AddItemIfNotEmpty("gender", graphData.Gender);
			userData.AddItemIfNotEmpty("birthday", graphData.Birthday);
			return userData;
		}
	}
}