-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFacebookGraphAPI.cs
More file actions
435 lines (392 loc) · 18.7 KB
/
FacebookGraphAPI.cs
File metadata and controls
435 lines (392 loc) · 18.7 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using Newtonsoft.Json.Linq;
namespace FB_API_Plugin
{
/// <summary>
/// A client for the Facebook Graph API.
///
/// See http://developers.facebook.com/docs/api for complete documentation
/// for the API.
///
/// The Graph API is made up of the objects in Facebook (e.g., people, pages,
/// events, photos) and the connections between them (e.g., friends,
/// photo tags, and event RSVPs). This client provides access to those
/// primitive types in a generic way. For example, given an OAuth access
/// token, this will fetch the profile of the active user and the list
/// of the user's friends:
///
/// var facebook = new FacebookGraphAPI(args["access_token"]);
/// var user = facebook.GetObject("me", null);
/// var friends = facebook.GetConnections("me", "friends", null);
///
/// You can see a list of all of the objects and connections supported
/// by the API at http://developers.facebook.com/docs/reference/api/.
///
/// You can obtain an access token via OAuth or by using the Facebook
/// JavaScript SDK. See http://developers.facebook.com/docs/authentication/
/// </summary>
internal class FacebookGraphApi
{
string _accessToken = null;
public FacebookGraphApi(string accessToken)
{
_accessToken = accessToken;
FBversion = "v1.0";
ProxyStr = "";
}
/// <summary>
/// Fetchs the given object from the graph.
/// </summary>
/// <param name="id">Id of the object to fetch</param>
/// <param name="args">List of arguments</param>
/// <returns>The required object</returns>
public JObject GetObject(string id, Dictionary<string, string> args)
{
return Request(id, args, null);
}
/// <summary>
/// Fetchs all of the given object from the graph.
/// </summary>
/// <param name="args"></param>
/// <param name="ids">Ids of the objects to return</param>
/// <returns>
/// A map from ID to object. If any of the IDs are invalid, an exception is raised
/// </returns>
public JObject GetObjects(Dictionary<string, string> args, params string[] ids)
{
var joinedIds = "";
for (var i = 0; i < ids.Length; i++) if (i == 0) joinedIds += ids[i]; else joinedIds += "," + ids[i];
if (args == null) args = new Dictionary<string, string>();
args["ids"] = joinedIds;
return Request("", args, null);
}
/// <summary>
/// Fetchs the connections for given object.
/// </summary>
/// <param name="id">Id of the object to fetch</param>
/// <param name="connectionName">Name of the connection</param>
/// <param name="args">List of arguments</param>
/// <returns>A JObject containing the required connections</returns>
public JObject GetConnections(string id, string connectionName, Dictionary<string, string> args)
{
return Request(id + "/" + connectionName, args, null);
}
/// <summary>
/// Writes the given object to the graph, connected to the given parent.
///
/// For example,
///
/// var data = new Dictionary<string, string>();
/// data.Add("message", "Hello, world");
/// facebook.PutObject("me", "feed", data);
///
/// writes "Hello, world" to the active user's wall.
///
/// See http://developers.facebook.com/docs/api#publishing for all of
/// the supported writeable objects.
///
/// Most write operations require extended permissions. For example,
/// publishing wall posts requires the "publish_stream" permission. See
/// http://developers.facebook.com/docs/authentication/ for details about
/// extended permissions.
/// </summary>
/// <param name="parentObject">The parent object</param>
/// <param name="connectionName">The connection name</param>
/// <param name="data">Post data</param>
/// <returns>A JObject with the result of the operation</returns>
public JObject PutObject(string parentObject, string connectionName, Dictionary<string, string> data)
{
if (_accessToken == null) throw new FacebookGraphApiException("Authentication", "Access Token Required");
return Request(parentObject + "/" + connectionName, null, data);
}
/// <summary>
/// Writes a wall post to current user wall.
///
/// We default to writing to the authenticated user's wall if no
/// profile_id is specified.
///
/// attachment adds a structured attachment to the status message being
/// posted to the Wall. It should be a dictionary of the form:
///
/// {"name": "Link name"
/// "link": "http://www.example.com/",
/// "caption": "{*actor*} posted a new review",
/// "description": "This is a longer description of the attachment",
/// "picture": "http://www.example.com/thumbnail.jpg"}
/// </summary>
/// <param name="message">The message to put on the wall</param>
/// <param name="attachment">Optional attachment for the message</param>
/// <returns>A JObject with the result of the operation</returns>
public JObject PutWallPost(string message, Dictionary<string, string> attachment)
{
if (attachment == null) attachment = new Dictionary<string, string>();
attachment.Add("message", message);
return PutObject("me", "feed", attachment);
}
/// <summary>
/// Writes a wall post to the given profile's wall.
///
/// We default to writing to the authenticated user's wall if no
/// profile_id is specified.
///
/// attachment adds a structured attachment to the status message being
/// posted to the Wall. It should be a dictionary of the form:
///
/// {"name": "Link name"
/// "link": "http://www.example.com/",
/// "caption": "{*actor*} posted a new review",
/// "description": "This is a longer description of the attachment",
/// "picture": "http://www.example.com/thumbnail.jpg"}
/// </summary>
/// <param name="message">The message to put on the wall</param>
/// <param name="attachment">Optional attachment for the message</param>
/// <param name="profileId">The profile where the message is goint to be put</param>
/// <returns>A JObject with the result of the operation</returns>
public JObject PutWallPost(string message, Dictionary<string, string> attachment, string profileId)
{
if (attachment == null) attachment = new Dictionary<string, string>();
attachment.Add("message", message);
return PutObject(profileId, "feed", attachment);
}
/// <summary>
/// Writes the given comment on the given post.
/// </summary>
/// <param name="objectId">Id of the object</param>
/// <param name="message">Message</param>
/// <returns>A JObject with the result of the operation</returns>
public JObject PutComment(string objectId, string message)
{
var args = new Dictionary<string, string>();
args.Add("message", message);
return PutObject(objectId, "comments", args);
}
/// <summary>
/// Likes the given post.
/// </summary>
/// <param name="objectId">Id of the object to be like</param>
/// <returns>A JObject with the result of the operation</returns>
public JObject PutLike(string objectId)
{
return PutObject(objectId, "likes", new Dictionary<string, string>());
}
/// <summary>
/// Deletes the object with the given ID from the graph.
/// </summary>
/// <param name="id">Id of the object to delete</param>
/// <returns>A JObject with the result of the operation</returns>
public JObject DeleteObject(string id)
{
var postArgs = new Dictionary<string, string>();
postArgs.Add("method", "delete");
return Request(id, null, postArgs);
}
/// <summary>
/// Fetches the given path in the Graph API.
///
/// Translates args to a valid query string. If post_args is given,
/// sends a POST request to the given path with the given arguments.
/// </summary>
/// <param name="path">The path where the request is to be send</param>
/// <param name="args">The Query arguments</param>
/// <param name="postArgs">The POST arguments</param>
/// <returns>A JObject of the facebook response</returns>
private JObject Request(string path, Dictionary<string, string> args, Dictionary<string, string> postArgs)
{
if (args == null) args = new Dictionary<string, string>();
if (_accessToken != null)
{
if (postArgs != null) postArgs["access_token"] = _accessToken;
else args["access_token"] = _accessToken;
}
string postData = null;
if (postArgs != null) postData = EncodeDictionary(postArgs);
var reply = "";
using (var wc = new WebClient())
{
if (ProxyStr != string.Empty)
{
var proxy = ProxyStr.Split(':');
wc.Proxy = new WebProxy(proxy[0], Convert.ToInt32(proxy[1]));
if (proxy.Length == 4)
{
wc.Proxy.Credentials = new NetworkCredential(proxy[2], proxy[3]);
}
}
wc.Encoding = Encoding.UTF8;
try
{
if (postArgs == null)
{
reply = wc.DownloadString("https://graph.facebook.com/" + (FBversion == "none" ? string.Empty : FBversion + "/") + path + (EncodeDictionary(args) != string.Empty ? "?" + EncodeDictionary(args) : ""));
}
else
{
if (args == null)
{
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
reply = wc.UploadString("https://graph.facebook.com/" + (FBversion == "none" ? string.Empty : FBversion + "/") + path, "POST", postData);
}
else
{
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
reply = wc.UploadString("https://graph.facebook.com/" + (FBversion == "none" ? string.Empty : FBversion + "/") + path + "?" + EncodeDictionary(args), "POST", postData);
}
}
}
catch (WebException ex)
{
var sr = new StreamReader(ex.Response.GetResponseStream(), wc.Encoding);
reply = sr.ReadToEnd();
}
}
JsonText = reply;
if ((reply.Trim() == "true")||(reply.Trim() == "false"))
{
var rep = "{\"result\": {\"bool\": \"" + reply.Trim() + "\"}}";
reply = rep;
}
var jo = JObject.Parse(reply);
if (jo["error"] != null)
throw new FacebookGraphApiException(jo["error"]["type"].ToString(),
jo["error"]["message"].ToString());
return jo;
}
/// <summary>
/// Encodes a dictionary keys to send them via HTTP request
/// </summary>
/// <param name="dict">Dictionary to be encoded</param>
/// <returns>Encoded dictionary keys</returns>
private string EncodeDictionary(Dictionary<string, string> dict)
{
var ret = "";
if (dict == null) return ret;
foreach (var item in dict)
ret += HttpUtility.UrlEncode(item.Key) + "=" + HttpUtility.UrlEncode(item.Value) + "&";
ret = ret.TrimEnd('&');
return ret;
}
/// <summary>
///
/// cookies should be a dictionary-like object mapping cookie names to
/// cookie values.
///
/// If the user is logged in via Facebook, we return a dictionary with the
/// keys "uid" and "access_token". The former is the user's Facebook ID,
/// and the latter can be used to make authenticated requests to the Graph API.
/// If the user is not logged in, we return None.
///
/// </summary>
/// <param name="cookies">HttpCookieCollection</param>
/// <param name="appId">Facebook Application Id</param>
/// <param name="appSecret">Facebook Application Secret</param>
/// <returns>Dictionary with the keys "uid" and "access_token"</returns>
public static Dictionary<string, string> GetUserFromCookie(HttpCookieCollection cookies, string appId, string appSecret)
{
var args = new Dictionary<string, string>();
var fbsig = HttpUtility.UrlDecode(cookies["fbs_" + appId].Value.Trim('"')).Split('&');
foreach (var s in fbsig)
{
var tmp = s.Split('=');
args.Add(tmp[0], tmp[1]);
}
var sortedArgs = (from entry in args orderby entry.Key ascending select entry);
var payload = "";
foreach (var item in sortedArgs) if (item.Key != "sig") payload += item.Key + "=" + item.Value;
var sig = Md5Hash(payload + appSecret);
var expires = int.Parse(args["expires"]);
var epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
if (sig == args["sig"] && (expires == 0 || epoch < expires)) return args;
return null;
}
/// <summary>
/// Parses the get parameters sent on canvas iframe.
///
/// You need to enable the OAuth 2.0 for Canvas option in
/// Application -> Edit Settings -> Advanced -> Migrations
///
/// If the user is logged in via Facebook, we return a dictionary with the
/// keys "uid" and "access_token". The former is the user's Facebook ID,
/// and the latter can be used to make authenticated requests to the Graph API.
/// If the user is not logged in, we return None.
/// </summary>
/// <param name="request">HttpRequest</param>
/// <param name="appId">Facebook Application Id</param>
/// <param name="appSecret">Facebook Application Secret</param>
/// <returns>Dictionary with the keys "uid" and "access_token"</returns>
public static Dictionary<string, string> GetUserFromQueryString(HttpRequest request, string appId, string appSecret)
{
var args = new Dictionary<string, string>();
//var signed_request = request.QueryString["signed_request"]; // Deprecate, POST for Canvas disabled
var signedRequest = request.Form["signed_request"];
if (signedRequest == null) return null;
var splitted = signedRequest.Split('.');
var encodedSig = splitted[0];
var payload = splitted[1];
var sig = Base64UrlDecode(encodedSig);
var jObject = JObject.Parse(Base64UrlDecode(payload));
var algorithm = jObject["algorithm"].ToString().ToUpper().Trim('"');
if (algorithm != "HMAC-SHA256") return null;
var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(appSecret));
hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(payload));
var encoding = new UTF8Encoding();
var expectedSig = encoding.GetString(hmacsha256.Hash);
if (sig != expectedSig) return null;
var data = new Dictionary<string, string>();
data.Add("user_id", (string)jObject["user_id"] ?? "");
data.Add("uid", (string)jObject["user_id"] ?? "");
data.Add("oauth_token", (string)jObject["oauth_token"] ?? "");
data.Add("access_token", (string)jObject["oauth_token"] ?? "");
var expires = ((long?) jObject["expires"] ?? 0);
data.Add("expires", expires > 0 ? expires.ToString() : "") ;
data.Add("profile_id", (string)jObject["profile_id"] ?? "");
return data;
}
/// <summary>
/// Performs base 64 url safe decoding
/// </summary>
/// <param name="str">The string to be decoded</param>
/// <returns></returns>
private static string Base64UrlDecode(string str)
{
var encoding = new UTF8Encoding();
var decodedJson = str.Replace("=", string.Empty).Replace('-', '+').Replace('_', '/');
var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 - decodedJson.Length % 4) % 4, '='));
return encoding.GetString(base64JsonArray);
}
/// <summary>
/// Gets the MD5 Hash string for the given input
/// </summary>
/// <param name="input">Input string</param>
/// <returns>Hashed string</returns>
private static string Md5Hash(string input)
{
var data = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder();
for (var i = 0; i < data.Length; i++) sb.Append(data[i].ToString("x2"));
return sb.ToString();
}
public string JsonText { get; set; }
public string FBversion { get; set; }
public string ProxyStr { get; set; }
}
/// <summary>
/// FacebookGraphAPIException
/// </summary>
internal class FacebookGraphApiException : Exception
{
public string Type { get; set; }
public new string Message { get; set; }
public FacebookGraphApiException(string type, string message)
{
Type = type;
Message = message;
}
}
}