Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial youtube calling

  • Loading branch information...
commit 07105fc983b0d847d8ea2aa8545070173a369b83 1 parent 8537908
@benjreinhart benjreinhart authored
View
8 components/oauth/controller.coffee
@@ -1,5 +1,5 @@
OAuthSession = require './datastore'
-{getTokens, googleOAuthUrl} = require './lib/google_oauth'
+{resetTokens, googleOAuthUrl} = require './lib/google_oauth'
exports.authenticate = (_, res) ->
res.redirect googleOAuthUrl
@@ -7,11 +7,11 @@ exports.authenticate = (_, res) ->
exports.authorize = (req, res) ->
{code} = req.query
- getTokens code, (err, response) ->
- return res.json 500, { err } if err?
+ resetTokens code, (err, response) ->
+ return res.json 500, { error: 'Error getting access tokens' } if err?
return res.json 500, { response } if response.error?
(new OAuthSession response).save (err) ->
- return res.json 500, { err, message: 'Error persisting session' } if err?
+ return res.json 500, { error: 'Error persisting session' } if err?
res.json 200, { message: 'Successfully authenticated' }
View
28 components/oauth/lib/google_oauth.coffee
@@ -1,18 +1,30 @@
{OAuth2Client} = require 'googleapis'
{google_apis: config} = require "#{ process.env.APP_ROOT }/core/config"
-oauth2Client = new OAuth2Client(
- config.client_id,
- config.client_secret,
- config.redirect_uri
-)
+exports.OAuthClient = OAuthClient =
+ new OAuth2Client(
+ config.client_id,
+ config.client_secret,
+ config.redirect_uri
+ )
exports.googleOAuthUrl =
- oauth2Client.generateAuthUrl
+ OAuthClient.generateAuthUrl
scope : config.scope
access_type : config.access_type
response_type : config.response_type
approval_prompt : config.approval_prompt
-exports.getTokens = (code, cb) ->
- oauth2Client.getToken code, cb
+TOKENS = {}
+
+exports.getTokens = -> TOKENS
+
+exports.resetTokens = (code, cb) ->
+ OAuthClient.getToken code, (err, response) ->
+ return cb err if err?
+
+ TOKENS =
+ access_token: response.access_token
+ refresh_token: response.refresh_token
+
+ cb null, response
View
8 components/videos/controller.coffee
@@ -0,0 +1,8 @@
+YouTube = require './lib/youtube'
+
+exports.show = (req, res) ->
+ YouTube.getVideo req.params.id, (err, video) ->
+ return res.send 500, { error: 'An error has occurred' } if err?
+
+ res.send 200, { video }
+
View
21 components/videos/lib/youtube.coffee
@@ -0,0 +1,21 @@
+{APP_ROOT} = process.env
+googleAPIs = require 'googleapis'
+
+{OAuthClient, getTokens} = require "#{ APP_ROOT }/components/oauth/lib/google_oauth"
+
+YouTube = null
+
+googleAPIs.load 'youtube', 'v3', (err, client) ->
+ throw err if err?
+
+ YouTube = client.youtube
+
+
+exports.getVideo = (id, cb) ->
+ OAuthClient.credentials = getTokens()
+
+ request = YouTube.videos.list({ id, part: 'snippet' }).withAuthClient(OAuthClient)
+ request.execute (err, response) ->
+ return cb err if err?
+
+ cb null, response.items[0]
View
6 core/routes.coffee
@@ -4,6 +4,7 @@
Users = requireController 'users'
OAuth = requireController 'oauth'
+Videos = requireController 'videos'
Promises = requireController 'promises'
##########
@@ -20,4 +21,7 @@ namespace 'api', ({ get, post }) ->
namespace 'oauth', ({ get }) ->
get '/authenticate', OAuth.authenticate
- get '/authorize', OAuth.authorize
+ get '/authorize', OAuth.authorize
+
+namespace 'videos', ({ get }) ->
+ get '/:id', Videos.show
View
BIN  videos/grpn_p4rty.MOV
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.