我有一个应用程序,允许用户从spotify流式传输歌曲。所以,为了实现这一点,我需要不时地更新会话,每当用户想要通过spotify播放歌曲时。我正在使用最新的spotify sdk(beta-9),我目前正在从https://www.youtube.com/watch?v=GeO00YdJ3cE以下教程。在该教程中,我们需要刷新令牌交换,但是当我从https://developer.spotify.com/technologies/spotify-ios-sdk/tutorial/看时,不需要刷新令牌交换。如何续订Spotify会话?
,我最终没有使用令牌交换,当我刷新然后我会播放歌曲以新的会话,我得到了以下错误:
Error Domain=com.spotify.ios-sdk.playback Code=8 "Login to Spotify failed because of invalid credentials." UserInfo=0x7f840bf807b0 {NSLocalizedDescription=Login to Spotify failed because of invalid credentials.}
而且我用下面这段代码,用于更新我的会议:
let userDefaults = NSUserDefaults.standardUserDefaults()
if let sessionObj : AnyObject = NSUserDefaults.standardUserDefaults().objectForKey("spotifySession") {
let sessionDataObj : NSData = sessionObj as! NSData
let session = NSKeyedUnarchiver.unarchiveObjectWithData(sessionDataObj) as! SPTSession
self.playUsingSession(session)
if !session.isValid() {
SPTAuth.defaultInstance().renewSession(session, callback: { (error : NSError!, newsession : SPTSession!) -> Void in
if error == nil {
let sessionData = NSKeyedArchiver.archivedDataWithRootObject(session)
userDefaults.setObject(sessionData, forKey: "spotifySession")
userDefaults.synchronize()
self.session = newsession
self.playUsingSession(newsession)
}else{
println("renew session having problerm >>>>> \(error)")
}
})
}else{
println("session is still valid")
self.playUsingSession(session)
}
}else{
spotifyLoginButton.hidden = false
}
和下面的代码流Spotify的歌曲:
func playUsingSession(sessionObj:SPTSession!){
if spotifyPlayer == nil {
spotifyPlayer = SPTAudioStreamingController(clientId: kSpotifyClientID)
}
spotifyPlayer?.loginWithSession(sessionObj, callback: { (error : NSError!) -> Void in
if error != nil {
println("enabling playback got error : \(error)")
return
}
var spotifyTrackUri : NSURL = NSURL(string: "spotify:track:3FREWTEY2uFxOorJZMmZPX")!
self.spotifyPlayer!.playURIs([spotifyTrackUri], fromIndex: 0, callback: { (error : NSError!) -> Void in
if error != nil {
println("\(error)")
}
})
})
}
我是否还需要刷新令牌交换最新sdk?或者是我的代码有什么缺失?
如何使用。 iOS应用中的herokuapp代码? – Parthpatel1105
这非常棒。对此感激不尽!但我收到:'JSON文本没有以数组或对象和选项开始,以允许片段未设置。“任何想法? –
'使用上面创建的服务器的URL添加以下内容时配置您的SPTAuth.defaultInstance():'你可以解释更多 – Jack