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