From e358219ec50ec30503e4e69361be5ba3a2dbe990 Mon Sep 17 00:00:00 2001 From: Dustyn Tubbs Date: Sat, 7 Jul 2018 09:59:21 -0700 Subject: [PATCH] Update --- package-lock.json | 15 ++++++++++----- package.json | 1 + src/api/youtubeAPI.js | 44 +++++++++++++++++++++++++++++-------------- src/server/main.js | 6 ++++++ 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 46668da..216be1d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "socialmonitorgraphql", + "name": "su-youtube-graphql-server", "version": "0.0.0", "lockfileVersion": 1, "requires": true, @@ -2709,10 +2709,9 @@ } }, "dotenv": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-2.0.0.tgz", - "integrity": "sha1-vXWcNXqqcDZeAclrewvsCKbg2Uk=", - "dev": true + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-6.0.0.tgz", + "integrity": "sha512-FlWbnhgjtwD+uNLUGHbMykMOYQaTivdHEmYwAKFjn6GKe/CqY0fNae93ZHTd20snh9ZLr8mTzIL9m0APQ1pjQg==" }, "duplexer": { "version": "0.1.1", @@ -8328,6 +8327,12 @@ "which": "^1.2.9" } }, + "dotenv": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-2.0.0.tgz", + "integrity": "sha1-vXWcNXqqcDZeAclrewvsCKbg2Uk=", + "dev": true + }, "fsevents": { "version": "1.0.17", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.0.17.tgz", diff --git a/package.json b/package.json index db662ee..3785b48 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "cors": "^2.8.3", "dataloader": "^1.3.0", "debug": "~2.6.0", + "dotenv": "^6.0.0", "elasticsearch": "^13.2.0", "express": "~4.14.1", "express-graphql": "^0.6.3", diff --git a/src/api/youtubeAPI.js b/src/api/youtubeAPI.js index 5741d0a..648b94d 100644 --- a/src/api/youtubeAPI.js +++ b/src/api/youtubeAPI.js @@ -1,22 +1,39 @@ var Promise = require('promise'); var google = require('googleapis'); var OAuth2 = google.auth.OAuth2; -var oauth2Client = new OAuth2(); -function youtubeAPI(tokens, resolveName, id, args){ - - oauth2Client.setCredentials({ - access_token:'', - refresh_token:'' - //expiry_date: true - }); +var oauth2Client = new OAuth2( + process.env.CLIENT_ID, + process.env.CLIENT_SECRET, + process.env.CALLBACK +); + +function youtubeAPI(context, resolveName, id, args){ + + let authorization = JSON.parse(context.headers.authorization) - var youtube = google.youtube({ - version: 'v3', - auth:oauth2Client - }); - return new Promise((resolve,reject) =>{ + + let unauthorized = !authorization || !authorization.accessToken + || !authorization.refreshToken; + + + if (unauthorized) { + reject(new Error('Unauthorized Request')); + } + + + oauth2Client.setCredentials({ + access_token: authorization.accessToken, + refresh_token: authorization.refreshToken + //expiry_date: true + }); + + var youtube = google.youtube({ + version: 'v3', + auth:oauth2Client + }); + switch(resolveName){ case 'search': @@ -34,7 +51,6 @@ function youtubeAPI(tokens, resolveName, id, args){ console.log(error); reject(error); }else{ - //console.log(data); resolve(data.items); } }); diff --git a/src/server/main.js b/src/server/main.js index 06ac718..8f6cd54 100644 --- a/src/server/main.js +++ b/src/server/main.js @@ -1,3 +1,4 @@ +require('dotenv').config(); const cors = require('cors'); const express = require('express'); const graphqlHTTP = require('express-graphql'); @@ -5,6 +6,11 @@ const youtubeSchema = require('../schema'); const logger = require('morgan'); +if (!process.env.CLIENT_ID || !process.env.CLIENT_SECRET || !process.env.CALLBACK) { + console.error('Error, Environment is missing a CLIENT_KEY, CLIENT_SECRET, and CALLBACK necessary to access the Twitter API'); + process.exit(1); +} + const app = express() app.use(cors());