facebook-oauth.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* appjet:version 0.1 */
  2. /* appjet:library */
  3. // Copyright (c) 2010, Herbert Vojčík
  4. // Licensed by MIT license (http://www.opensource.org/licenses/mit-license.php)
  5. import("facebook", "storage");
  6. var _u = import({}, "lib-support/useful");
  7. fb.OAuthInterface = function () {
  8. }
  9. fb.OAuthInterface.prototype.restApi = function (method, args) {
  10. // return wget("https://api.facebook.com/"+method+this.tokenString(), args);
  11. print("https://api.facebook.com/method/"+method, this.withToken(args));
  12. print(wget("https://api.facebook.com/method/"+method, this.withToken(args), {complete:true}));
  13. response.stop();
  14. };
  15. fb.OAuthInterface.prototype.graphApiGet = function (path, args) {
  16. return wget("https://graph.facebook.com/"+path, this.withToken(args));
  17. };
  18. fb.OAuthInterface.prototype.graphApiPost = function (path, args) {
  19. return wpost("https://graph.facebook.com/"+path, this.withToken(args));
  20. };
  21. fb.OAuthInterface.prototype.withToken = function (args) {
  22. var token = this._token || (this._token = this.obtainToken());
  23. return token ? _u.extend({access_token:token}, args) : args;
  24. };
  25. fb.free = new fb.OAuthInterface;
  26. fb.free.obtainToken = function () {};
  27. fb.offline = new fb.OAuthInterface;
  28. fb.offline.obtainToken = function () {
  29. var obtained = fb.free.graphApiPost("oauth/access_token", {
  30. grant_type: "client_credentials",
  31. client_id: storage.facebooklib.appId,
  32. client_secret: storage.facebooklib.secret
  33. }).split('=');
  34. if (obtained[0] === "access_token") { return obtained[1]; }
  35. };
  36. fb.online = new fb.OAuthInterface;
  37. fb.online.obtainToken = function () {
  38. var obtained = eval("("+fb.free.graphApiPost("oauth/exchange_sessions", {
  39. sessions: fb.sessionKey,
  40. client_id: storage.facebooklib.appId,
  41. client_secret: storage.facebooklib.secret
  42. })+")"); // XXX JSON.parse
  43. return obtained[0].access_token;
  44. };