javascript - Accessing the Spotify API data (i.e. tokens) from redirect_uri via nodejs then redirect to angular app -
hi i'm creating web application using node.js , angularjs. i'm new both technologies.
this app setup:
node.js + mongodb rest api - handle server side requests communicate spotify (http://localhost:3000)
angularjs - app (http://localhost:8080)
spotify has made it's spotify web api public. i'm trying retrieve access token (and refresh token) can make api calls them. able access_token & refresh_token, however, since i'm coming http://localhost:8080/test http://localhost:3000/spotifyapi/login initialize auth http://localhost:3000/spotifyapi/callback receive tokens.
if please help/guide me on how retrieve tokens/make api calls pass angularjs app, appreciated.
below codes:
i'm using grunt compile js 1 file in angular app
node.js api (http://localhost:3000/spotifyapi)
spotify.js
var spotifywebapi = require('spotify-web-api-node'); var request = require('request'); var querystring = require('querystring'); var client_id = config.spotify.client_id, client_secret = config.spotify.client_secret, redirect_uri = config.spotify.redirect_uri; var spotifyapi = new spotifywebapi({ client_id: client_id, redirect_uri: redirect_uri }); exports.login = function(req, res) { var scope = 'user-read-private playlist-read playlist-read-private', state = 'mixr-test'; res.redirect('https://accounts.spotify.com/authorize?' + querystring.stringify({ response_type: 'code', client_id: client_id, scope: scope, redirect_uri: redirect_uri, state: state })); }; exports.callback = function(req, res) { var code = req.query.code; var authoptions = { url: 'https://accounts.spotify.com/api/token', form: { code: code, redirect_uri: redirect_uri, grant_type: 'authorization_code', client_id: client_id, client_secret: client_secret }, json: true }; request.post(authoptions, function(error, response, body) { if (!error && response.statuscode == 200) { var access_token = body.access_token, refresh_token = body.refresh_token; var options = { url: 'https://api.spotify.com/v1/me', headers: { 'authorization': 'bearer ' + access_token }, json: true }; // res.send(options); res.redirect('http://localhost:8080/#/test#' + querystring.stringify({ access_token: access_token, refresh_token: refresh_token })); } res.send({ access_token: access_token, refresh_token: refresh_token }); }); }; exports.refreshtoken = function(req, res) { var refresh_token = req.query.refresh_token; var authoptions = { url: 'https:/accounts.spotify.com/api/token', headers: { 'authorization': 'basic ' + (new buffer(client_id + ':' + client_secret).tostring('base64')) }, form: { grant_type: 'refresh_token', refresh_token: refresh_token }, json: true }; request.post(authoptions, function(error, response, body) { if (!error && response.statuscode === 200) { var access_token = body.access_token; res.send({ 'access_token': access_token }); } }); };
routemap.js
module.exports = [ ['/spotifyapi', 'spotify#index', 'get'], ['/spotifyapi/login', 'spotify#login', 'get'], ['/spotifyapi/callback', 'spotify#callback', 'get'], ['/spotifyapi/refreshtoken', 'spotify#refreshtoken', 'get'], ];
angularjs app (http://localhost:8080/test)
spotifyservice.js
app.factory('spotifyservice', function($http) { var apibaseurl = "http://localhost:3000"; return { login: function(config) { return $http.get(apibaseurl + '/spotifyapi/login'); }, callback: function(access_token, refresh_token) { return $http.get(apibaseurl + '/spotifyapi/callback', {access_token:access_token, refresh_token:refresh_token}); }, refresh: function(refresh_token) { return $http.get(apibaseurl + '/spotifyapi/refreshtoken', {access_token: access_token, refresh_token: refresh_token}); }, getusers: function(){ return http.get(apibaseurl + '/users'); } }; });
spotifyctrl.js
app.controller('testctrl', ['$scope', 'spotifyservice', function($scope, spotifyservice) { $scope.access_token = ''; $scope.refresh_token = ''; var getlogin = function() { spotifyservice.login().success(function(data){ spotifyservice.callback(access_token, refresh_token).success(function(data){ $scope.access_token = data.access_token; $scope.refresh_token = data.refresh_token; }).error(function(data){ console.log("error!" + data); }); }).error(function(data){ console.log("error!" + data); }); }; } ]);
spotify.jade
block content div#container(ng-controller="spotifyctrl") a(href="http://localhost:3000/spotifyapi/login" class="btn btn-large") login via spotify p {{ access_token }} p {{ refresh_token }}
i'm able go spotify's login page -> authenticate. redirect_uri http://localhost:3000/spotifyapi/callback. when authentication finished, i'm able access_token , refresh_token i'm still on node.js api server , can't value angular service pass view. how redirect redirect_uri angular app , able retrieve access , refresh tokens via angular services?
i apologize lengthy post, i'm in road block right on how complete module.
thank in advance!
Comments
Post a Comment