我正在使用xinranxiao开发一个Angular-Meteor应用程序:spotify-web-api包,它是用spotify-node-apixinranxiao:meteor-accounts-spotify包构建的,用spotify登录 .

我可以通过创建Spotify Web Api提供的客户端ID和客户端密码以及仅仅 http://localhost:3000/_oauth/spotify?close 的重定向uri来成功登录spotify . 我有一个页面,其中列出了用户播放列表,并显示了专辑图片 . 就在专辑作品的正下方,我发现's frame player with the link to the above album/playlist but there'是 401 error. Need authentication .

我已登录但仍需要身份验证?

如果我在我的Chrome浏览器控制台中输入 Meteor.loginWithSpotify() ,我会得到未定义的,但是我没有看到我的访问令牌?如何获取访问令牌并刷新?我相信这是它的原因显示我仍然需要身份验证,为什么如果我输入 Meteor.loginWithSpotify() 我得到 undefined .

在我的服务器端Meteor代码我有:

ServiceConfiguration.configurations.update(
  { "service": "spotify" },
  {
    $set: {
      "clientId": "000000000000000000000000000000",
      "secret": "000000000000000000000000000000"
    }
  },
  { upsert: true }
)

在我的客户端Meteor Methods中,我有一个getUserPlaylists方法,它可以完成它的工作 . 下面是一个方法checkTokenRefreshed,我仍然不确定它是否刷新令牌?:

Meteor.methods({

  // Get a user's playlists
  getUserPlaylists: function() {
    //Spotify call
    var spotifyApi = new SpotifyWebApi()
    //response object
    var userplaylists = spotifyApi.getUserPlaylists(Meteor.user().profile.id,function(err,data){
      if(err){
        console.log("Retrieval error ", err)
      }
      else{
        console.log("Success, your playlist ", data.body)
      }
    })
    //Need to refresh token
    if(checkTokenRefreshed(userplaylists, spotifyApi)){
      userplaylists = spotifyApi.getUserPlaylists(Meteor.user().profile.id,function(err,data){
        if(err){
          console.log("Retrieval error ", err)
        }
        else{
          console.log("Success, your playlist ", data.body)
        }
      })//end response
    }//end checkTokenRefreshed

    return userplaylists

  }//end getUserPlaylists

})//end Meteor.methods

var checkTokenRefreshed = function(response, api) {
  if (response.error && response.error.statusCode === 401) {
    api.refreshAndUpdateAccessToken();
    return true
  } else {
    return false
  }
}

在我的客户端代码我有:

var options = {
  showDialog: true, // Whether or not to force the user to approve the app again if they’ve already done so.
  requestPermissions: ['user-read-email','playlist-modify-private', 'user-library-read','user-follow-read', 'playlist-read-private','streaming'] // Spotify access scopes.
};

var scopes = ['user-read-email','playlist-modify-private', 'user-library-read','user-follow-read', 'playlist-read-private','streaming']
Accounts.ui.config({'requestPermissions':{'spotify':scopes}})

Meteor.loginWithSpotify(options, function(accessToken) {
  console.log('accessToken is ', accessToken)
});

客户端代码的最后一个方法应该将我的accessToken打印到控制台,而是打印 accessToken is undefined


我应该提一下我可以使用node / meteor-spotify-web-api spotifyApi.getUserPlaylists()方法访问用户播放列表,但是如果我不能在不收到401错误的情况下发布链接,那会有什么用呢?

xinranxiao的文档:meteor-spotify-api说2)通过xinranxiao:accounts-spotify或直接通过这个API(参考here . )获取oauth access_token .