Advertisement
rg443

node-feedly.js

Aug 24th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var qs = require("querystring"), OAuth2 = require("oauth").OAuth2;
  2. function Feedly(config) {
  3.   this.baseUrl = config.feedly.baseUrl;
  4.   this.authenticateUrl = config.feedly.authenticateUrl;
  5.   this.clientId = config.secrets.clientId;
  6.   this.redirectUrl = config.secrets.redirectUrl;
  7.   this.scope = config.feedly.scope;
  8.   this.oauth = new OAuth2(config.secrets.clientId, config.secrets.clientSecret, config.feedly.baseUrl, config.feedly.authenticateUrl, config.feedly.accessTokenUrl, null);
  9.   this.oauth.useAuthorizationHeaderforGET(true);
  10.   this.oauth.setAuthMethod("OAuth");
  11. }
  12. Feedly.prototype.getAuthClientRedirectUrl = function() {
  13.   return this.baseUrl + this.authenticateUrl + "?client_id=" + this.clientId + "&response_type=code&redirect_uri=" + this.redirectUrl + "&scope=" + this.scope;
  14. };
  15. Feedly.prototype.getAccessToken = function(code, params, callback) {
  16.   var params = params || {};
  17.   params.grant_type = "authorization_code";
  18.   params.redirect_uri = this.redirectUrl;
  19.   this.oauth.getOAuthAccessToken(code, params, function(err, access_token, refresh_token, results) {
  20.     if (err) {
  21.       callback(err, undefined);
  22.     }
  23.     try {
  24.       console.log(results);
  25.       callback(null, access_token);
  26.     } catch (e) {
  27.       callback(e);
  28.     }
  29.   });
  30. };
  31. Feedly.prototype.getProfile = function(params, error, success) {
  32.   var path = "profile";
  33.   var url = this.baseUrl + path;
  34.   this.doRequest(url, error, success);
  35. };
  36. Feedly.prototype.getPreferences = function(params, error, success) {
  37.   var path = "preferences";
  38.   var url = this.baseUrl + path;
  39.   this.doRequest(url, error, success);
  40. };
  41. Feedly.prototype.getCategories = function(params, error, success) {
  42.   var path = "categories";
  43.   var url = this.baseUrl + path;
  44.   this.doRequest(url, error, success);
  45. };
  46. Feedly.prototype.getSubscriptions = function(params, error, success) {
  47.   var path = "subscriptions";
  48.   var url = this.baseUrl + path;
  49.   this.doRequest(url, error, success);
  50. };
  51. Feedly.prototype.getTopics = function(params, error, success) {
  52.   var path = "topics";
  53.   var url = this.baseUrl + path;
  54.   this.doRequest(url, error, success);
  55. };
  56. Feedly.prototype.getTags = function(params, error, success) {
  57.   var path = "tags";
  58.   var url = this.baseUrl + path;
  59.   this.doRequest(url, error, success);
  60. };
  61. Feedly.prototype.getFeed = function(params, error, success) {
  62.   var path = "feed/" + params.feedId;
  63.   var url = this.baseUrl + path;
  64.   this.doRequest(url, error, success);
  65. };
  66. Feedly.prototype.getStreamIds = function(params, error, success) {
  67.   console.log(encodeURIComponent(params.streamId));
  68.   var path = "streams/" + encodeURIComponent(params.streamId) + "/ids";
  69.   var url = this.baseUrl + path;
  70.   this.doRequest(url, error, success);
  71. };
  72. Feedly.prototype.getStreamContents = function(params, error, success) {
  73.   console.log(encodeURIComponent(params.streamId));
  74.   var path = "streams/" + encodeURIComponent(params.streamId) + "/contents";
  75.   var url = this.baseUrl + path;
  76.   this.doRequest(url, error, success);
  77. };
  78. Feedly.prototype.getEntry = function(params, error, success) {
  79.   var path = "entries/" + params.entryId;
  80.   var url = this.baseUrl + path;
  81.   this.doRequest(url, error, success);
  82. };
  83. Feedly.prototype.doRequest = function(url, error, success) {
  84.   this.oauth.get(url, this.accessToken, function(err, body, response) {
  85.     if (!err && response.statusCode == 200) {
  86.       success(body);
  87.     } else {
  88.       error(err, response, body);
  89.     }
  90.   });
  91. };
  92. if (!(typeof exports === "undefined")) {
  93.   exports.Feedly = Feedly;
  94. }
  95. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement