facebook-oauth.js 1.7 KB

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