Displaying permissions with spotify
I'm using spotify-web-api-node to authorize a user so I can create and modify a users playlist.
The app works as it should. Where by the user can be directed to the spotify accounts page,be authorized, and then be redirected to my app.
But when on the Spotify authorize page it doesn't display the permissions(scope) sought by the application . I'm guessing it should look like the image displayed HERE on the Spotify website. Im not too sure if its a case that my code isn't complete or there might be an issue with the Spotify authorize page when using spotify-web-api-node.
Edit
app.js
var SpotifyWebApi = require('spotify-web-api-node');
var http = require('http'),
fs = require('fs'),
index = fs.readFileSync(__dirname + '/index.html');
// Send index.html to all requests
var app = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(index);
});
var scopes = ['playlist-modify-public', 'playlist-modify-private'],
redirectUri = 'http://localhost:3000',
clientId = '',
clientSecret = '',
state = 'some-state-of-my-choice';
// Setting credentials can be done in the wrapper's constructor.
var spotifyApi = new SpotifyWebApi({
redirectUri : redirectUri,
clientId : clientId,
clientSecret : clientSecret
});
// Create the authorization URL
var authorizeURL = spotifyApi.createAuthorizeURL(scopes, state);
console.log(authorizeURL);
// Socket.io server listens to our app
var io = require('socket.io').listen(app);
io.sockets.on('connection', function (socket){
io.sockets.emit('url',{value:authorizeURL});
//once code is extracted from client-side get tokens
socket.on('code', function(data){
var code = data.x;
// get tokens
spotifyApi.authorizationCodeGrant(code)
.then(function(data) {
console.log('The token expires in ' + data['expires_in']);
console.log('The access token is ' + data['access_token']);
console.log('The refresh token is ' + data['refresh_token']);
console.log(code);
}, function(err) {
console.log('Something went wrong with getting Auth tokens !', err);
});
});
});app.listen(3000);
html
<body>
<a><button>Authorize</button></a>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src='//localhost:3000/socket.io/socket.io.js'></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.1.min.js"></script>')</script>
<script>
function getParameterByName(name) {
name = name.replace(/[[]/, "[").replace(/[]]/, "]");
var regex = new RegExp("[?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/+/g, " "));
}
$( document ).ready(function() {
var socket = io.connect('//localhost:3000');
var code = getParameterByName('code');
socket.emit('code', { x: code});
socket.on('url', function(data) {
$('a').attr('href',data.value);
});
});
</script>
</body>
There's a test that checks that the AuthorizeURL includes scopes. Make sure that the scopes you're sending in is in an array. Your code should look something like this (obviously with scopes, redirectUri, clientId, and state changed to your application's values)
var scopes = ['user-read-private', 'user-read-email'],
redirectUri = 'https://example.com/callback',
clientId = '5fe01282e44241328a84e7c5cc169165',
state = 'some-state-of-my-choice';
var api = new SpotifyWebApi({
clientId : clientId,
redirectUri : redirectUri
});
var authorizeURL = api.createAuthorizeURL(scopes, state);
console.log(authorizeURL); // "https://accounts.spotify.com/authorize?client_id=5fe01282e44241328a84e7c5cc169165&response_type=code&redirect_uri=https://example.com/callback&scope=user-read-private%20user-read-email&state=some-state-of-my-choice"
Hope this helps! If not, please include your code in your question.
链接地址: http://www.djcxy.com/p/55672.html上一篇: Spotify Web API
下一篇: 用spotify显示权限