Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Refactor using OAuth2
Browse files Browse the repository at this point in the history
This breaks compatibility with previous version of node-rdio.
  • Loading branch information
siboulet committed Jul 3, 2015
1 parent 1950a9f commit 5e27dd9
Showing 1 changed file with 53 additions and 55 deletions.
108 changes: 53 additions & 55 deletions rdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,58 @@
* Homepage: http://github.com/dawnerd/
* License: MIT
*
*
* Config options:
* rdio_api_key
* rdio_api_shared
* callback_url - URL user will be redirected to upon login
*/

module.exports = function(config, oauth) {
if(typeof oauth == 'undefined') {
oauth = require('oauth').OAuth;
}

config.rdio_oauth_request = 'http://api.rdio.com/oauth/request_token';
config.rdio_oauth_access = 'http://api.rdio.com/oauth/access_token';
config.rdio_oauth_auth = 'https://www.rdio.com/oauth/authorize?oauth_token=';
config.rdio_api = 'http://api.rdio.com/1/';

//setup oauth
var oa = new oauth(
config.rdio_oauth_request,
config.rdio_oauth_access,
config.rdio_api_key,
config.rdio_api_shared,
"1.0", config.callback_url, "HMAC-SHA1");

//public methods
return {
getRequestToken: function(callback) {
oa.getOAuthRequestToken(callback);
},
getAccessToken: function(auth_token, auth_token_secret, oauth_verifier, callback) {
oa.getOAuthAccessToken(auth_token, auth_token_secret, oauth_verifier, callback);
},
getPlaybackToken: function(auth_token, auth_token_secret, host, callback) {
this.api(
auth_token,
auth_token_secret,
{
method: 'getPlaybackToken',
domain: encodeURIComponent(host)
},
callback
);
},

api: function(auth_token, auth_token_secret, data, callback) {
oa.post(
config.rdio_api,
auth_token,
auth_token_secret,
data,
null,
callback
);
}
};
};
var extend = require('util')._extend;
var querystring = require('querystring');

var OAuth2 = require('oauth').OAuth2;

var Rdio = function(config) {
extend(this, config);

this.oauth2 = new OAuth2(
this.clientId,
this.clientSecret,
'https://services.rdio.com/',
null,
'oauth2/token',
null
);
}

Rdio.prototype.login = function(callback) {
var self = this;

this.oauth2.getOAuthAccessToken(this.refreshToken, {
grant_type: 'refresh_token',
}, function(err, accessToken, refreshToken) {
if (err) return callback((err.data) ? new Error(JSON.parse(err.data).error_description) : err);
self.accessToken = accessToken;
self.refreshToken = refreshToken;
callback();
});
}

Rdio.prototype.call = function(method, args, callback) {
if (typeof args === 'function') {
callback = args;
args = {};
}

var headers = {
'Authorization': this.oauth2.buildAuthHeader(this.accessToken),
'Content-Type': 'application/x-www-form-urlencoded',
};

var data = querystring.stringify(extend(args, {method: method}));

this.oauth2._request('POST', 'https://services.rdio.com/api/1/', headers, data, null, function(err, data) {
if (err) return callback((err.data) ? new Error(JSON.parse(err.data).error_description) : err);
data = JSON.parse(data);
if (data.status !== 'ok') return callback(new Error(data.message));
callback(null, data.result);
});
};

module.exports = Rdio;

0 comments on commit 5e27dd9

Please sign in to comment.