我正在使用node.js服务器,Spotify API和spotify-web-api-js节点模块来创建一个Web应用程序,用户可以在其中输入艺术家的名字,查看相关艺术家的歌曲列表,然后可选择将该播放列表保存到自己的Spotify帐户.但是,我仍然在最后一步遇到麻烦.
我的用户授权流程首先发生:
if (params.access_token) {
s.setAccessToken(params.access_token);
s.getMe().then(function(data) {
console.log(data);
console.log(data.id);
user_id = data.id;
Run Code Online (Sandbox Code Playgroud)
下面是API收集歌曲的实际细节的地方.尽管它较低,但它首先出现,并且仅当用户单击该页面上的第二个按钮时才会发生用户授权.
async.times(counter, function(n, next){
s.getArtistTopTracks(relatedArtists[n].id, "US", function (err, data2) {
relatedArtists[n].song = data2.tracks[0].name;
relatedArtists[n].uri = data2.tracks[0].uri;
console.log(relatedArtists[n].uri);
// make sure to put the access token here add song to playlist
// create array
song_uris.push(relatedArtists[n].uri);
console.log(song_uris);
// song_uris = relatedArtists[n].uri;
//
// console.log(song_uris);
next(null);
$("#playlist").load(function() {
s.addTracksToPlaylist(user_id, playlist_id, song_uris);
});
});
}, function(err) {
// console.table(relatedArtists);
for (k = 0; k < 20; k++)
{ …
Run Code Online (Sandbox Code Playgroud) 登录在Chrome上运行正常,但在Safari(或我假设其他Webkit浏览器)上不起作用.登录后我收到此错误消息("您想要的更改被拒绝.也许您尝试更改了您无法访问的内容."):
根据我的heroku日志,这就是发生的事情:
2016-12-07T14:14:23.778153+00:00 app[web.1]: Can't verify CSRF token authenticity
2016-12-07T14:14:23.778899+00:00 app[web.1]: Completed 422 Unprocessable Entity in 2ms (ActiveRecord: 0.0ms)
2016-12-07T14:14:23.785544+00:00 app[web.1]:
2016-12-07T14:14:23.785547+00:00 app[web.1]: ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
Run Code Online (Sandbox Code Playgroud)
我相信我正在发送正确的CSRF令牌,但似乎有些事情发生了故障.这是我目前的application_controller.rb
:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
after_action :flash_to_headers
# this is so that json requests don't redirect without a user
before_action :authenticate_user!
# before_action :authenticate_user!, unless: request.format == :json
# before_action :user_needed, if: …
Run Code Online (Sandbox Code Playgroud) 几天前,我能够成功安装Postgresql并从SQLite创建/迁移我的数据库(准备部署我的Rails 4应用程序)...或者我认为.
我重新启动了我的服务器,但当我尝试访问我的应用程序时,出现此错误:
PG::ConnectionBad
could not connect to server: Connection refused
Is the server running locally and accepting connections on Unix domain socket
"/var/run/postgresql/.s.PGSQL.5432"?
Run Code Online (Sandbox Code Playgroud)
我在SO上看过几个类似的回答问题,但它们都涉及Mac.由于我正在使用Cloud 9 IDE,我认为可能存在差异(例如,提及localhost
可能不相关,因为这不是我访问当前在c9.io上的应用程序的方式.)
根据其他问题的答案,我在我的终端尝试了这个:
sudo ps auxw | grep post
Run Code Online (Sandbox Code Playgroud)
这给了我:
ubuntu 2527 0.0 0.0 10552 888 pts/5 S+ 15:21 0:00 grep --color=auto post
Run Code Online (Sandbox Code Playgroud)
然后我试过:
sudo find / -name .s.PGSQL.5432 -ls
Run Code Online (Sandbox Code Playgroud)
我得到了:
5286 0 lrwxrwxrwx 1 root root 26 May 11 15:13 /var/pgsql_socket/.s.PGSQL.5432 -> /private/tmp/.s.PGSQL.5432
5183 0 srwxrwxrwx 1 postgres postgres 0 May 10 …
Run Code Online (Sandbox Code Playgroud) 为了在我的Web应用程序中获取Spotify API的访问令牌(由他们的Web授权流程指定),我了解到我必须提出POST
请求.但是,当我这样做时,我得到了XMLHttpRequest
500 Error
由于跨源问题.
我已经想出了如何允许CORS GET
请求,但我不知道如何对POST
请求执行相同的操作.此链接提供配置提示,但它保留实际路由GET
和POST
空白.
这是我的Express.js服务器的相关代码:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.static(__dirname + '/public')); // looks in public directory, not root directory (protects files)
app.get('/', function(req, res) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.send(__dirname + '\\index.html')
});
app.post('/', function(req, res) {
res.send(req.body.spotify);
});
Run Code Online (Sandbox Code Playgroud)
(spotify
是spotify-web-api-js
节点模块).
我以前曾尝试将确切的代码复制app.get
到app.post
,但这导致服务器崩溃.
这是我程序的JavaScript文件中的一些代码,它打算POST
在用户单击一个按钮后发送请求,该按钮将它们带到Spotify的授权路径的开头并批准登录:
$('#spotify').on('click', …
Run Code Online (Sandbox Code Playgroud) jquery ×2
spotify ×2
asynchronous ×1
cloud9-ide ×1
cors ×1
csrf ×1
devise ×1
express ×1
javascript ×1
node.js ×1
postgresql ×1
ruby ×1
safari ×1