facebook-oauth.js 1.6 KB

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