Tôi có một đăng nhập Spotify trong ứng dụng của tôi và cố gắng thực hiện một AutoLogin:Spotify quản lý phiên
Đăng nhập chức năng
func getSpotifyToken(fromController controller: UIViewController, success: (spotifyToken: String?) -> Void, failure: (error: NSError?) -> Void) {
loginSuccessBlock = success
loginFailureBlock = failure
SPTAuth.defaultInstance().clientID = SpotifyClientID
SPTAuth.defaultInstance().redirectURL = NSURL(string: SpotifyRedirectURI)
SPTAuth.defaultInstance().requestedScopes = [SPTAuthStreamingScope, SPTAuthPlaylistReadPrivateScope]
let spotifyLoginController = SPTAuthViewController.authenticationViewController()
spotifyLoginController.delegate = self
spotifyLoginController.clearCookies {() -> Void in
controller.presentViewController(spotifyLoginController, animated: true, completion: nil)
}
}
Kiểm tra xem phiên tồn tại
private func spotifyConnected() -> Bool {
if SPTAuth.defaultInstance().session == nil {
self.loadSpotifySession()
}
return SPTAuth.defaultInstance().session != nil
}
Lưu phiên
private func saveSpotifySession() {
let sessionData = NSKeyedArchiver.archivedDataWithRootObject(SPTAuth.defaultInstance().session)
NSUserDefaults.standardUserDefaults().setObject(sessionData, forKey: Spotify_Session_Key)
NSUserDefaults.standardUserDefaults().synchronize()
}
tải phiên
private func loadSpotifySession() {
if let sessionData = NSUserDefaults.standardUserDefaults().objectForKey(Spotify_Session_Key) as? NSData {
let session = NSKeyedUnarchiver.unarchiveObjectWithData(sessionData) as! SPTSession
SPTAuth.defaultInstance().session = session
}
}
Renew phiên - gọi tại ứng dụng bắt đầu
func renewSpotifySession() {
guard spotifyConnected() else {
return
}
SPTAuth.defaultInstance().renewSession(SPTAuth.defaultInstance().session) { (error: NSError!, session: SPTSession!) -> Void in
if session != nil {
SPTAuth.defaultInstance().session = session
} else {
print("Failed to refresh spotify session")
}
}
}
renewSession trở lại con số không. Tôi đã thấy một số thông tin về refreshToken, Nhưng tôi không biết, nơi tôi có thể nắm bắt nó.
Làm cách nào để có thể gia hạn phiên spotify? có lẽ tôi đã làm sai điều gì?
Theo tôi được biết , Tôi cần phải thực hiện chức năng trao đổi và làm mới trên máy chủ của tôi và thêm các url này vào SPTAuth(). Spotify sẽ tự động cập nhật mã thông báo của tôi bằng cách sử dụng chương trình phụ trợ của tôi? – Viktor
Tôi sẽ không nói tự động - bạn vẫn cần định kỳ kiểm tra xem bạn có mã thông báo hợp lệ không và làm mới nó nếu cần. Mã thông báo hết hạn sau 60 phút. – brki
Đúng, nếu tôi sai: Tôi thêm chức năng hoán đổi và gia hạn trên máy chủ và thêm url cho chúng vào STPAuth(). Sau khi đăng nhập, tôi sẽ nhận được accessToken với thời gian hết hạn 1h. Nếu tôi đóng khách hàng của mình và mở lại sau 2h và gọi phiên "loginOrRenewSession" sẽ được khôi phục? Hoặc tôi cần định kỳ thủ công mã thông báo truy cập cho tất cả người dùng trên máy chủ? – Viktor