facebook-oauth.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. fb.OAuthInterface.prototype.restApi = function (method, args) {
  9. return wpost("https://api.facebook.com/method/"+method, this.withToken(args));
  10. };
  11. fb.OAuthInterface.prototype.graphApiGet = function (path, args) {
  12. return wget("https://graph.facebook.com/"+path, this.withToken(args));
  13. };
  14. fb.OAuthInterface.prototype.graphApiPost = function (path, args) {
  15. return wpost("https://graph.facebook.com/"+path, this.withToken(args));
  16. };
  17. fb.OAuthInterface.prototype.withToken = function (args) {
  18. var token = this._token || (this._token = this.obtainToken());
  19. return token ? _u.extend({access_token:token}, args) : args;
  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("oauth/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("oauth/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. };