我发现 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