Minor updates

- Rename secret_key to client_secret
- Fetch values from new fields
- Authorization Code: Exchange code by token sending id and secret
  in body instead of Base64 header
This commit is contained in:
jperez 2014-06-03 13:26:18 +02:00
parent 684ad7dae1
commit 0d7878f84f
6 changed files with 19 additions and 17 deletions

View file

@ -32,5 +32,5 @@ Go to [My Applications on Spotify Developer](https://developer.spotify.com/my-ap
* http://localhost:8888 (needed for the implicit grant flow) * http://localhost:8888 (needed for the implicit grant flow)
* http://localhost:8888/callback * http://localhost:8888/callback
Once you have created your app, replace the `client_id`, `redirect_uri` and `secret_key` in the examples with the ones you get from My Applications. Once you have created your app, replace the `client_id`, `redirect_uri` and `client_secret` in the examples with the ones you get from My Applications.

View file

@ -12,7 +12,7 @@ var request = require('request'); // "Request" library
var querystring = require('querystring'); var querystring = require('querystring');
var client_id = '03ffe0cac0a0401aa6673c3cf6d02ced'; // Your client id var client_id = '03ffe0cac0a0401aa6673c3cf6d02ced'; // Your client id
var secret_key = 'a57c43efb9644574a96d6623fb8bfbc2'; // Your secret key var client_secret = 'a57c43efb9644574a96d6623fb8bfbc2'; // Your client secret
var redirect_uri = 'http://localhost:8888/callback'; // Your redirect uri var redirect_uri = 'http://localhost:8888/callback'; // Your redirect uri
var app = express(); var app = express();
@ -38,16 +38,16 @@ app.get('/callback', function(req, res) {
var code = req.query.code; var code = req.query.code;
var authOptions = { var authOptions = {
url: 'https://accounts.spotify.com/api/token', url: 'https://accounts.spotify.com/api/token',
headers: {
'Authorization': 'Basic ' + (new Buffer(client_id + ':' + secret_key).toString('base64'))
},
form: { form: {
code: code, code: code,
redirect_uri: redirect_uri, redirect_uri: redirect_uri,
grant_type: 'authorization_code' grant_type: 'authorization_code',
client_id: client_id,
client_secret: client_secret
}, },
json: true json: true
}; };
request.post(authOptions, function(error, response, body) { request.post(authOptions, function(error, response, body) {
if (!error && response.statusCode === 200) { if (!error && response.statusCode === 200) {
@ -81,7 +81,7 @@ app.get('/refresh_token', function(req, res) {
var refresh_token = req.query.refresh_token; var refresh_token = req.query.refresh_token;
var authOptions = { var authOptions = {
url: 'https://accounts.spotify.com/api/token', url: 'https://accounts.spotify.com/api/token',
headers: { 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + secret_key).toString('base64')) }, headers: { 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64')) },
form: { form: {
grant_type: 'refresh_token', grant_type: 'refresh_token',
refresh_token: refresh_token refresh_token: refresh_token
@ -99,4 +99,5 @@ app.get('/refresh_token', function(req, res) {
}); });
}); });
console.log('Listening on 8888');
app.listen(8888); app.listen(8888);

View file

@ -35,16 +35,16 @@
<h1>Logged in as {{display_name}}</h1> <h1>Logged in as {{display_name}}</h1>
<div class="media"> <div class="media">
<div class="pull-left"> <div class="pull-left">
<img class="media-object" width="150" src="{{image.url}}" /> <img class="media-object" width="150" src="{{images.0.url}}" />
</div> </div>
<div class="media-body"> <div class="media-body">
<dl class="dl-horizontal"> <dl class="dl-horizontal">
<dt>Display name</dt><dd>{{display_name}}</dd> <dt>Display name</dt><dd>{{display_name}}</dd>
<dt>Id</dt><dd>{{id}}</dd> <dt>Id</dt><dd>{{id}}</dd>
<dt>Email</dt><dd>{{email}}</dd> <dt>Email</dt><dd>{{email}}</dd>
<dt>Spotify URI</dt><dd><a href="{{self.uri}}">{{self.uri}}</a></dd> <dt>Spotify URI</dt><dd><a href="{{external_urls.spotify}}">{{external_urls.spotify}}</a></dd>
<dt>Link</dt><dd><a href="{{self.web}}">{{self.web}}</a></dd> <dt>Link</dt><dd><a href="{{href}}">{{href}}</a></dd>
<dt>Profile Image</dt><dd><a href="{{image.url}}">{{image.url}}</a></dd> <dt>Profile Image</dt><dd><a href="{{images.0.url}}">{{images.0.url}}</a></dd>
</dl> </dl>
</div> </div>
</div> </div>

View file

@ -10,14 +10,14 @@
var request = require('request'); // "Request" library var request = require('request'); // "Request" library
var client_id = '03ffe0cac0a0401aa6673c3cf6d02ced'; // Your client id var client_id = '03ffe0cac0a0401aa6673c3cf6d02ced'; // Your client id
var secret_key = 'a57c43efb9644574a96d6623fb8bfbc2'; // Your secret key var client_secret = 'a57c43efb9644574a96d6623fb8bfbc2'; // Your client secret
var redirect_uri = 'http://localhost:8888/callback'; // Your redirect uri var redirect_uri = 'http://localhost:8888/callback'; // Your redirect uri
// your application requests authorization // your application requests authorization
var authOptions = { var authOptions = {
url: 'https://accounts.spotify.com/api/token', url: 'https://accounts.spotify.com/api/token',
headers: { headers: {
'Authorization': 'Basic ' + (new Buffer(client_id + ':' + secret_key).toString('base64')) 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
}, },
form: { form: {
grant_type: 'client_credentials' grant_type: 'client_credentials'

View file

@ -10,4 +10,5 @@
var express = require('express'); // Express web server framework var express = require('express'); // Express web server framework
var app = express(); var app = express();
app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/public'));
console.log('Listening on 8888');
app.listen(8888); app.listen(8888);

View file

@ -34,16 +34,16 @@
<h1>Logged in as {{display_name}}</h1> <h1>Logged in as {{display_name}}</h1>
<div class="media"> <div class="media">
<div class="pull-left"> <div class="pull-left">
<img class="media-object" width="150" src="{{image.url}}" /> <img class="media-object" width="150" src="{{images.0.url}}" />
</div> </div>
<div class="media-body"> <div class="media-body">
<dl class="dl-horizontal"> <dl class="dl-horizontal">
<dt>Display name</dt><dd>{{display_name}}</dd> <dt>Display name</dt><dd>{{display_name}}</dd>
<dt>Id</dt><dd>{{id}}</dd> <dt>Id</dt><dd>{{id}}</dd>
<dt>Email</dt><dd>{{email}}</dd> <dt>Email</dt><dd>{{email}}</dd>
<dt>Spotify URI</dt><dd><a href="{{self.uri}}">{{self.uri}}</a></dd> <dt>Spotify URI</dt><dd><a href="{{external_urls.spotify}}">{{external_urls.spotify}}</a></dd>
<dt>Link</dt><dd><a href="{{self.web}}">{{self.web}}</a></dd> <dt>Link</dt><dd><a href="{{href}}">{{href}}</a></dd>
<dt>Profile Image</dt><dd><a href="{{image.url}}">{{image.url}}</a></dd> <dt>Profile Image</dt><dd><a href="{{images.0.url}}">{{images.0.url}}</a></dd>
</dl> </dl>
</div> </div>
</div> </div>