getToken --> getAccessToken

This commit is contained in:
Todd Treece 2015-07-27 18:08:08 -04:00
parent 24bcf515db
commit 14f1c3fa6e
4 changed files with 19 additions and 17 deletions

View file

@ -9,10 +9,7 @@ var NPR = API({
password: process.env.NPR_PASSWORD
});
NPR.authenticate(function(err, token) {
if(err)
return console.error(err);
NPR.authenticate(function(token) {
if(! token)
return console.error('invalid token');

View file

@ -1,7 +1,8 @@
/************************ DEPENDENCIES *****************************/
var util = require('util'),
OAuth = require('oauth'),
bunyan = require('bunyan');
bunyan = require('bunyan'),
request = require('request');
var proto = API.prototype;
exports = module.exports = API;
@ -33,23 +34,24 @@ proto.token_endpoint = 'authorization/v2/token';
/***************************** AUTH *******************************/
proto.authenticate = function(cb) {
this.getToken()
.then(function(args) {
cb.apply(this, args);
})
this.getAccessToken()
.then(cb)
.catch(function(err) {
this.log.error(err);
cb(err);
cb();
}.bind(this));
};
proto.getToken = function() {
proto.getAccessToken = function() {
var self = this;
return new Promise(function(resolve, reject) {
if(self.access_token)
return resolve(self.access_token);
if(! self.client_id)
return reject('OAuth Client ID not supplied');
@ -83,7 +85,7 @@ proto.getToken = function() {
self.log.debug('refresh token', self.refresh_token);
self.log.debug('results', results);
return resolve([err, access_token, refresh_token, results]);
return resolve(access_token);
};
@ -99,3 +101,8 @@ proto.getToken = function() {
};
/*********************** RECOMMENDATIONS **************************/

View file

@ -34,6 +34,7 @@
"dependencies": {
"bunyan": "^1.4.0",
"dotenv": "^1.2.0",
"oauth": "^0.9.13"
"oauth": "^0.9.13",
"request": "^2.60.0"
}
}

View file

@ -13,10 +13,7 @@ describe('NPR API', function() {
password: process.env.NPR_PASSWORD
});
NPR.authenticate(function(err, token) {
if(err)
done(err);
NPR.authenticate(function(token) {
if(! token)
return done('invalid token');