尝试使用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时我不能查看任何值??? :(
答案 0 :(得分:1)
我将manifest.json
API版本更改为:
"api": "1.3.0"
现在我可以从会话中获取信息,例如我的国家:
var userCountry = models.session.country;
答案 1 :(得分:1)
我发现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