Browse Source

OAuth calls to rest API and Graph API added

Herbert Vojčík 14 years ago
parent
commit
dcc94f96f5
2 changed files with 50 additions and 1 deletions
  1. 1 1
      lib-app/facebook-ext.js
  2. 49 0
      lib-app/facebook-oauth.js

+ 1 - 1
lib-app/facebook-ext.js

@@ -3,7 +3,7 @@
 // Copyright (c) 2009, 2010, Herbert Vojčík
 // Licensed by MIT license (http://www.opensource.org/licenses/mit-license.php)
 
-import("facebook", "storage");
+import("facebook", "lib-app/facebook-oauth", "storage");
 
 // TODO new FBML tags
 importTags(this, [

+ 49 - 0
lib-app/facebook-oauth.js

@@ -0,0 +1,49 @@
+/* appjet:version 0.1 */
+/* appjet:library */
+// Copyright (c) 2010, Herbert Vojčík
+// Licensed by MIT license (http://www.opensource.org/licenses/mit-license.php)
+
+import("facebook", "storage");
+
+fb.OAuthInterface = function () {
+}
+
+fb.OAuthInterface.prototype.restApi = function (method, args) {
+  return wget("https://api.facebook.com/"+method+this.tokenString(), args);
+};
+
+fb.OAuthInterface.prototype.graphApiGet = function (path, args) {
+  return wget("https://graph.facebook.com/"+path+this.tokenString(), args);
+};
+
+fb.OAuthInterface.prototype.graphApiPost = function (path, args) {
+  return wpost("https://graph.facebook.com/"+path+this.tokenString(), args);
+};
+
+fb.OAuthInterface.prototype.tokenString = function () {
+  var token = this._token || (this._token = this.obtainToken());
+  return token ? "?access_token="+token : "";
+};
+
+fb.free = new fb.OAuthInterface;
+fb.free.obtainToken = function () {};
+
+fb.offline = new fb.OAuthInterface;
+fb.offline.obtainToken = function () {
+  var obtained = fb.free.graphApiPost("access_token", {
+    grant_type: "client_credentials",
+    client_id: storage.facebooklib.appId,
+    client_secret: storage.facebooklib.secret
+  }).split('=');
+  if (obtained[0] === "access_token") { return obtained[1]; }
+};
+
+fb.online = new fb.OAuthInterface;
+fb.online.obtainToken = function () {
+  var obtained = eval("("+fb.free.graphApiPost("exchange_sessions", {
+    sessions: fb.sessionKey,
+    client_id: storage.facebooklib.appId,
+    client_secret: storage.facebooklib.secret
+  })+")"); // XXX JSON.parse
+  return obtained[0].access_token;
+};