facebook-oauth.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 wpost("https://api.facebook.com/method/"+method, this.withToken(args));
  11. };
  12. fb.OAuthInterface.prototype.graphApiGet = function (path, args) {
  13. return wget("https://graph.facebook.com/"+path, this.withToken(args));
  14. };
  15. fb.OAuthInterface.prototype.graphApiPost = function (path, args) {
  16. return wpost("https://graph.facebook.com/"+path, this.withToken(args));
  17. };
  18. fb.OAuthInterface.prototype.withToken = function (args) {
  19. var token = this._token || (this._token = this.obtainToken());
  20. return token ? _u.extend({access_token:token}, args) : args;
  21. };
  22. fb.free = new fb.OAuthInterface;
  23. fb.free.obtainToken = function () {};
  24. fb.offline = new fb.OAuthInterface;
  25. fb.offline.obtainToken = function () {
  26. var obtained = fb.free.graphApiPost("oauth/access_token", {
  27. grant_type: "client_credentials",
  28. client_id: storage.facebooklib.appId,
  29. client_secret: storage.facebooklib.secret
  30. }).split('=');
  31. if (obtained[0] === "access_token") { return obtained[1]; }
  32. };
  33. fb.online = new fb.OAuthInterface;
  34. fb.online.obtainToken = function () {
  35. var obtained = eval("("+fb.free.graphApiPost("oauth/exchange_sessions", {
  36. sessions: fb.sessionKey,
  37. client_id: storage.facebooklib.appId,
  38. client_secret: storage.facebooklib.secret
  39. })+")"); // XXX JSON.parse
  40. return obtained[0].access_token;
  41. };