【问题标题】:Spotify API 1.x get user sessionSpotify API 1.x 获取用户会话
【发布时间】:2013-03-28 14:28:05
【问题描述】:

试用 Spotify API 1.x。 我的清单

"Dependencies": {
      "api": "1.20.1",
      "views": "1.24.1"
  }

使用新的 spotify API 获取当前会话时遇到问题。 Session Docs

过了一会儿,我得到了用户信息:

require(['$api/models','$api/models#User','$api/models#Session'], function(models) {
    var user = models.User.fromURI('spotify:user:@');
    user.load('username', 'name').done(function(u) {
        userUid = u.identifier;
    });
});

但是 Session 没有加载方法(出现抛出错误),并且在查看 models.Session 时我看不到任何值??? :(

【问题讨论】:

    标签: spotify


    【解决方案1】:

    我将manifest.json API 版本更改为:

    "api": "1.3.0"
    

    现在我可以从会话中获取信息,例如我的国家:

    var userCountry = models.session.country;
    

    【讨论】:

      【解决方案2】:

      我发现 Spotify 文档对 Sessions 有点误导。我通过反复试验和谷歌搜索而不是从文档中发现了很多东西。

      就像@Backer 所说,将 API 版本更改为 1.3.0(或更高版本,如果可用)。 请注意,您必须重新启动 Spotify 才能生效

      然后你可以像这样访问 Session 对象(这里,“session”必须小写):

      models.session.load('product','connection','device','user').done(function(s){
          console.log('sess:',s)
      });
      

      User 对象将成为其中的一部分,但除非您加载它们,否则它不会填充属性。下面是从 Session 和 User 检索属性子集的示例:

      require([
      '$api/models','$api/models#Session'
      ], function(models) {
      app.user = {};
          models.session.load('product','connection','device','user').done(function(sess){
             sess.user.load('name', 'username', 'subscribed').done(function(user){
                 app.user.name = user.name; // string
                 app.user.username = user.username; // string
                 app.user.subscribed = user.subscribed; // boolean
             });
             app.user.connection = sess.connection; // string
             app.user.country = sess.country; // string ISO-2
             app.user.device = sess.device; // string
             app.user.language = sess.language; // string ISO-2
             app.user.product = sess.product; // string
      
          });
      });
      

      整个 Session 对象:

      Session
      _done: 65535
      _listening: true
      _ob: Object
      _obcount: 1
      _requestArgs: Array[0]
      _requestName: "session_event_wait"
      capabilities: Object
      connecting: false
      connection: "wlan"
      country: "SE"
      developer: true
      device: "desktop"
      incognito: false
      language: "en"
      online: true
      product: "open"
      resolution: 1
      streaming: "enabled"
      testGroup: 000
      user: User
          _done: 255
          currentUser: true
          identifier: "longcodehere"
          image: "http://profile-images.scdn.co/artists/default/anotherlongcodehere"
          images: Array[2]
          name: "My Name"
          subscribed: false
          uri: "spotify:user:@"
          username: "myusername"
          __proto__: c
          __proto__: c
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-21
        • 1970-01-01
        相关资源
        最近更新 更多