我有一个应用程序,允许用户流歌曲从spotify。因此,为了实现这一点,每当用户想要从spotify流歌曲时,我都需要不时地更新会话。我正在使用最新的spotify sdk (beta-9),目前我正在遵循https://www.youtube.com/watch?v=GeO00YdJ3cE的教程。在本教程中,我们需要刷新令牌交换,但是当我从https://developer.spotify.com/technologies/spotify-ios-sdk/tutorial/查看时,不需要刷新令牌交换。
最后,我没有使用令牌交换,当我刷新我的会话,然后用更新的会话播放歌曲时,我得到了以下错误:
Error Domain=com.spotify.ios- Spotify . Code=8“登录到Spotify由于凭据无效而失败。”UserInfo=0x7f840bf807b0 {NSLocalizedDescription=Login to Spotify由于凭据无效而失败。}
我正在使用下面的代码来更新我的会话:
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的令牌交换吗?还是我的代码遗漏了什么?
发布于 2016-08-16 22:03:36
默认情况下,除非使用授权代码流,否则用户需要使用Spotify SDK每小时登录一次应用程序。要使用这个流,您需要设置一个服务器来处理令牌交换和刷新。
如果不是打电话
SPTAuth.defaultInstance().renewSession(SPTAuth.defaultInstance().session, callback: { (error, session) in
if let session = session {
SPTAuth.defaultInstance().session = session
}
})发布于 2018-07-16 23:29:35
我推荐遵循本教程:https://medium.com/@brianhans/getting-started-with-the-spotify-ios-sdk-435607216ecc和这个也是:https://medium.com/@brianhans/spotify-ios-sdk-authentication-b2c35cd4affb
完成之后,您将看到创建了一个名为"Constants.swift“的文件,如下所示:
import Foundation
struct Constants {
static let clientID = "XXXXXXXXXXXXXXXXXXXX"
static let redirectURI = URL(string: "yourappname://")!
static let sessionKey = "spotifySessionKey"
}然后,您可以按照Heroku中的步骤(不要惊慌失措地进入):
https://github.com/adamontherun/SpotifyTokenRefresh
当服务器“工作”时,几乎准备好了,回到Xcode项目并在"Constants.swift“文件中添加两个静态常量,如下所示:
import Foundation
struct Constants {
static let clientID = "XXXXXXXXXXXXXXXXXXXX"
static let redirectURI = URL(string: "yourappname://")!
static let sessionKey = "spotifySessionKey"
static let tokenSawp = URL(string: "https://yourappname.herokuapp.com/swap")
static let tokenRefresh = URL(string:"https://yourappname.herokuapp.com/refresh")
}要完成,请转到AppDelegate.swift并搜索"func setupSpotify()“。添加新的两个常量,您的函数应该如下所示:
func setupSpotify() {
SPTAuth.defaultInstance().clientID = Constants.clientID
SPTAuth.defaultInstance().redirectURL = Constants.redirectURI
SPTAuth.defaultInstance().sessionUserDefaultsKey = Constants.sessionKey
SPTAuth.defaultInstance().tokenSwapURL = Constants.tokenSawp //new constant added
SPTAuth.defaultInstance().tokenRefreshURL = Constants.tokenRefresh //new constant added
SPTAuth.defaultInstance().requestedScopes = [SPTAuthStreamingScope]
do {
try SPTAudioStreamingController.sharedInstance().start(withClientId: Constants.clientID)
} catch {
fatalError("Couldn't start Spotify SDK")
}
}作为最后一步,只需将SPTAuth.defaultInstance().renewSession添加到您的signInSpotify函数中,应该如下所示:
@IBAction func SignInSpotify(_ sender: Any) {
if SPTAuth.defaultInstance().session == nil {
let appURL = SPTAuth.defaultInstance().spotifyAppAuthenticationURL()
let webURL = SPTAuth.defaultInstance().spotifyWebAuthenticationURL()!
// Before presenting the view controllers we are going to start watching for the notification
NotificationCenter.default.addObserver(self,
selector: #selector(receievedUrlFromSpotify(_:)),
name: NSNotification.Name.Spotify.authURLOpened,
object: nil)
if SPTAuth.supportsApplicationAuthentication() {
UIApplication.shared.open(appURL!, options: [:], completionHandler: nil)
} else {
let webVC = SFSafariViewController(url: webURL)
present(webVC, animated: true, completion: nil)
}
} else if SPTAuth.defaultInstance().session.isValid() == true {
print("YOUR SESSION IS VALID")
self.successfulLogin()
} else {
print("YOUR SESSION IS NOT VALID / NEED RENEW")
//Every 60 minutes the token need a renew https://github.com/spotify/ios-sdk
SPTAuth.defaultInstance().renewSession(SPTAuth.defaultInstance().session, callback: { (error, session) in
if let session = session {
SPTAuth.defaultInstance().session = session
self.successfulLogin()
print("RENEW OK")
}
if let error = error {
print("RENEW NOT OK \(error)")
}
})
}
}祝好运!
https://stackoverflow.com/questions/31364319
复制相似问题