Skip to content

Commit

Permalink
Add module oauth20
Browse files Browse the repository at this point in the history
  • Loading branch information
tnovas committed Sep 28, 2017
1 parent 9da0c66 commit 2305f75
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 66 deletions.
61 changes: 9 additions & 52 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
let request = require('axios');
let oAuth2 = require('oauth20');

class Youtube {
class Youtube extends oAuth2 {
constructor(clientId, clientSecret, redirectUrl, key, scopes, channelId, accessToken='', refreshToken='') {
super(clientId, clientSecret, redirectUrl, scopes, 'https://accounts.google.com/o/oauth2/', 'auth', 'token');

this.__credentials = {
clientId: clientId,
clientSecret: clientSecret,
redirectUrl: redirectUrl,
key: key,
scopes: scopes,
channelId: channelId,
accessToken: accessToken,
refreshToken: refreshToken
channelId: channelId
};

this.__urlApi = {
base: 'https://www.googleapis.com/youtube/v3/',
baseAuth: 'https://accounts.google.com/o/oauth2/',
authorizate: 'auth',
accessTokenPath: 'token',
channels: 'channels',
streams: 'liveStreams'
};

request.defaults.headers.post['Authorization'] = `Bearer ${accessToken}`;
}

authorizationUrl() {
return `${this.__urlApi.baseAuth}${this.__urlApi.authorizate}?response_type=code&client_id=${this.__credentials.clientId}&redirect_uri=${this.__credentials.redirectUrl}&scope=${this.__credentials.scopes}`;
}

getCredentials() {
return {
accessToken: this.__credentials.accessToken,
refreshToken: this.__credentials.refreshToken,
channelId: this.__credentials.channelId
};
let credentials = super.getCredentials();
credentials.channelId = this.__credentials.channelId;

return credentials;
}

getChannel(success, error) {
Expand All @@ -48,25 +35,6 @@ class Youtube {
this.__get(url, qs, success, error);
}

connect(code, success, error) {
let url = `${this.__urlApi.baseAuth}${this.__urlApi.accessTokenPath}`;
let body = {
grant_type: 'authorization_code',
client_id: this.__credentials.clientId,
client_secret: this.__credentials.clientSecret,
redirect_uri: this.__credentials.redirectUrl,
code: code
};

this.__post(url, body, (result) => {
this.__credentials.accessToken = result.data.access_token;
this.__credentials.refreshToken = result.data.refresh_token;

request.defaults.headers.post['Authorization'] = `Bearer ${result.data.access_token}`;
success();
}, error);
}

__get(url, qs={}, success, error) {
request({
method: 'GET',
Expand All @@ -77,17 +45,6 @@ class Youtube {
.then(success)
.catch(error);
}

__post(url, body, success, error) {
request({
method: 'POST',
url: url,
body: body,
json: true
})
.then(success)
.catch(error);
}
}

module.exports = Youtube;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/tnovas/youtube"
},
"email": "tnovas@gmail.com",
"version": "1.0.0",
"version": "1.0.1",
"description": "This module use Youtube Data API V3 https://developers.google.com/youtube/v3/guides",
"main": "app.js",
"scripts": {
Expand All @@ -19,11 +19,11 @@
"chai": "^4.1.2",
"coveralls": "^2.13.1",
"istanbul": "^0.4.5",
"mocha": "^3.5.3",
"nock": "^9.0.14"
"mocha": "^3.5.3"
},
"dependencies": {
"axios": "^0.16.2",
"oauth20": "^1.0.3",
"request": "^2.82.0"
}
}
21 changes: 10 additions & 11 deletions tests/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,26 @@ describe('youtube', function() {

it('constructor() should make credentials with params', function() {
var credentials = {
clientId: "clientId",
clientSecret: "clientSecret",
redirectUrl: "redirectUrl",
key: "key",
scopes: "https://www.googleapis.com/auth/youtube.force-ssl,https://www.googleapis.com/auth/youtube,https://www.googleapis.com/auth/youtube.readonly,https://www.googleapis.com/auth/youtube.upload,https://www.googleapis.com/auth/youtubepartner-channel-audit",
channelId: "channelId",
accessToken: '',
refreshToken: '',

channelId: "channelId"
};

var urls = {
base: 'https://www.googleapis.com/youtube/v3/',
channels: 'channels',
streams: 'liveStreams'
};

expect(JSON.stringify(youtube.__credentials)).to.equal(JSON.stringify(credentials));
expect(JSON.stringify(youtube.__urlApi)).to.equal(JSON.stringify(urlApi));
expect(JSON.stringify(youtube.__urlApi)).to.equal(JSON.stringify(urls));
});

it('authorizationUrl() should return Url of authorization', function() {
expect(youtube.authorizationUrl()).to.equal(`${urlApi.baseAuth}${urlApi.authorizate}?response_type=code&client_id=${youtube.__credentials.clientId}&redirect_uri=${youtube.__credentials.redirectUrl}&scope=${youtube.__credentials.scopes}`);
});

it('connect() should connect to youtube and get accessToken with code', function() {
mock.onPost(`${urlApi.baseAuth}${urlApi.accessTokenPath}`).reply(200, {access_token: 'token'});
mock.onPost(`${urlApi.baseAuth}${urlApi.accessTokenPath}`).replyOnce(200, {access_token: 'token'});

youtube.connect('code', () => { expect('token').to.equal(youtube.__credentials.accessToken); }, (err) => {console.log(err)});
});
Expand All @@ -70,7 +69,7 @@ describe('youtube', function() {
}
];

mock.onGet(`${urlApi.base}${urlApi.channels}`).reply(200, items);
mock.onGet(`${urlApi.base}${urlApi.channels}`).replyOnce(200, items);

youtube.getChannel((result) => { expect(JSON.stringify(items[0])).to.equal(JSON.stringify(result.data[0])); }, (err) => {console.log(err)});
});
Expand Down

0 comments on commit 2305f75

Please sign in to comment.