Javascript 使用隐式授权的Spotify身份验证失败
我正在尝试使用所概述的隐式授权流与Spotify进行身份验证。我已经将localhost:3000和localhost:3000/callback/添加到我的用户开发仪表板(我还尝试了各种组合,例如localhost:3000/callback,以及我能想到的任何怪癖)。我正在使用Github页面上Spotify教程中提供的示例。以下是我的隐式授权身份验证设置: app.js:Javascript 使用隐式授权的Spotify身份验证失败,javascript,spotify,Javascript,Spotify,我正在尝试使用所概述的隐式授权流与Spotify进行身份验证。我已经将localhost:3000和localhost:3000/callback/添加到我的用户开发仪表板(我还尝试了各种组合,例如localhost:3000/callback,以及我能想到的任何怪癖)。我正在使用Github页面上Spotify教程中提供的示例。以下是我的隐式授权身份验证设置: app.js: /**
/**
* This is an example of a basic node.js script that performs
* the Implicit Grant oAuth2 flow to authenticate against
* the Spotify Accounts.
*
* For more information, read
* https://developer.spotify.com/web-api/authorization-guide/#implicit_grant_flow
*/
var express = require('express'); // Express web server framework
var app = express();
app.use(express.static(__dirname + '/public'));
console.log('Listening on 3000');
app.listen(3000);
index.html:
<!doctype html>
<html>
<head>
<title>Example of the Implicit Grant flow with Spotify</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<style type="text/css">
#login, #loggedin {
display: hidden;
}
.text-overflow {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 500px;
}
</style>
</head>
<body>
<div class="container">
<div id="login">
<h1>This is an example of the Implicit Grant flow</h1>
<button id="login-button" class="btn btn-primary">Log in with Spotify</button>
</div>
<div id="loggedin">
<div id="user-profile">
</div>
<div id="oauth">
</div>
</div>
</div>
<script id="user-profile-template" type="text/x-handlebars-template">
<h1>Logged in as {{display_name}}</h1>
<div class="media">
<div class="pull-left">
<img class="media-object" width="150" src="{{images.0.url}}" />
</div>
<div class="media-body">
<dl class="dl-horizontal">
<dt>Display name</dt><dd class="clearfix">{{display_name}}</dd>
<dt>Id</dt><dd>{{id}}</dd>
<dt>Email</dt><dd>{{email}}</dd>
<dt>Spotify URI</dt><dd><a href="{{external_urls.spotify}}">{{external_urls.spotify}}</a></dd>
<dt>Link</dt><dd><a href="{{href}}">{{href}}</a></dd>
<dt>Profile Image</dt><dd class="clearfix"><a href="{{images.0.url}}">{{images.0.url}}</a></dd>
<dt>Country</dt><dd>{{country}}</dd>
</dl>
</div>
</div>
</script>
<script id="oauth-template" type="text/x-handlebars-template">
<h2>oAuth info</h2>
<dl class="dl-horizontal">
<dt>Access token</dt><dd class="text-overflow">{{access_token}}</dd>
</dl>
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0-alpha.1/handlebars.min.js"></script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
(function() {
var stateKey = 'spotify_auth_state';
/**
* Obtains parameters from the hash of the URL
* @return Object
*/
function getHashParams() {
var hashParams = {};
var e, r = /([^&;=]+)=?([^&;]*)/g,
q = window.location.hash.substring(1);
while ( e = r.exec(q)) {
hashParams[e[1]] = decodeURIComponent(e[2]);
}
console.log(hashParams);
return hashParams;
}
/**
* Generates a random string containing numbers and letters
* @param {number} length The length of the string
* @return {string} The generated string
*/
function generateRandomString(length) {
var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
var userProfileSource = document.getElementById('user-profile-template').innerHTML,
userProfileTemplate = Handlebars.compile(userProfileSource),
userProfilePlaceholder = document.getElementById('user-profile');
oauthSource = document.getElementById('oauth-template').innerHTML,
oauthTemplate = Handlebars.compile(oauthSource),
oauthPlaceholder = document.getElementById('oauth');
var params = getHashParams();
var access_token = params.access_token,
state = params.state,
storedState = localStorage.getItem(stateKey);
console.log(access_token);
if (access_token && (state == null || state !== storedState)) {
console.log('reached null state or unstored state');
alert('There was an error during the authentication');
} else {
localStorage.removeItem(stateKey);
if (access_token) {
$.ajax({
url: 'https://api.spotify.com/v1/me',
headers: {
'Authorization': 'Bearer ' + access_token
},
success: function(response) {
userProfilePlaceholder.innerHTML = userProfileTemplate(response);
$('#login').hide();
$('#loggedin').show();
}
});
} else {
console.log('Second Canary');
$('#login').show();
$('#loggedin').hide();
}
document.getElementById('login-button').addEventListener('click', function() {
var client_id = 'Not for your eyes'; // Your client id
var redirect_uri = 'localhost:3000/callback/'; // Your redirect uri
var state = generateRandomString(16);
localStorage.setItem(stateKey, state);
var scope = 'user-read-private user-read-email';
var url = 'https://accounts.spotify.com/authorize';
url += '?response_type=token';
url += '&client_id=' + encodeURIComponent(client_id);
url += '&scope=' + encodeURIComponent(scope);
url += '&redirect_uri=' + encodeURIComponent(redirect_uri);
url += '&state=' + encodeURIComponent(state);
url += '&show_dialog=true';
console.log(url);
window.location = url;
console.log('clicked');
}, false);
}
})();
</script>
</html>
Spotify的隐式授权流示例
#登录,#loggedin{
显示:隐藏;
}
.文本溢出{
溢出:隐藏;
文本溢出:省略号;
空白:nowrap;
宽度:500px;
}
这是隐式授权流的一个示例
使用Spotify登录
以{display_name}身份登录
显示名称{Display_name}
Id{{Id}
电子邮件{{Email}
spotifyuri
链接
轮廓图像
国家{{Country}
oAuth信息
访问令牌{Access_token}
(功能(){
var stateKey='spotify_auth_state';
/**
*从URL的哈希中获取参数
*@返回对象
*/
函数getHashParams(){
var hashParams={};
变量e,r=/([^&;=]+)=?([^&;]*)/g,
q=window.location.hash.substring(1);
while(e=r.exec(q)){
hashParams[e[1]]=decodeURIComponent(e[2]);
}
console.log(hashParams);
返回hashParams;
}
/**
*生成包含数字和字母的随机字符串
*@param{number}length字符串的长度
*@return{string}生成的字符串
*/
函数生成器域字符串(长度){
var text='';
可能的变量='ABCDEFGHIjklmnopqrstuvxyzabCDEFGHIjklmnopqrstuvxyzo123456789';
对于(变量i=0;i
在批准我的应用程序后,我返回到URI,但没有结果。结果如下:
换句话说,我得到了我应该得到的回应(至少在我看来是这样)。但Firefox不知道如何使用它,Chrome希望我使用xdg open。任何帮助都将不胜感激。我现在已经开始工作了。首先,我更改了仪表板上的URL。我现在将它们设置为和。然后,我在app.js文件中添加了一条路由:
app.get('/callback', function(req, res) {
res.redirect('/');
});
这解决了我的问题,我现在可以正确地进行身份验证。您是否尝试在index.html上设置localhost:3000?奇怪的是Firefox不识别“localhost”。。。你的操作系统是什么?显式使用环回IP会导致Spotify的身份验证出错。我正在运行Ubuntu17.10。检查一下是否在/etc/hosts上定义了localhost,它应该是“127.0.0.1localhost”是的,我已经在我的hosts文件中找到了。