Spotify会话管理

Vik*_*tor 8 spotify ios swift

我在我的应用程序中有一个spotify登录并尝试进行自动登录:

登录功能

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

检查会话是否存在

private func spotifyConnected() -> Bool {        
    if SPTAuth.defaultInstance().session == nil {
        self.loadSpotifySession()
    }        
    return SPTAuth.defaultInstance().session != nil
}
Run Code Online (Sandbox Code Playgroud)

保存会议

private func saveSpotifySession() {
    let sessionData = NSKeyedArchiver.archivedDataWithRootObject(SPTAuth.defaultInstance().session)
    NSUserDefaults.standardUserDefaults().setObject(sessionData, forKey: Spotify_Session_Key)
    NSUserDefaults.standardUserDefaults().synchronize()
}
Run Code Online (Sandbox Code Playgroud)

加载会话

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
    }
}
Run Code Online (Sandbox Code Playgroud)

续订会话 - 在应用开始时调用

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")
        }
    }        
}
Run Code Online (Sandbox Code Playgroud)

renewSession返回零.我看到了一些关于refreshToken的信息,但我不知道,我能抓到它.

我如何更新spotify会话?也许我做错了什么?

brk*_*rki 5

为了在没有用户需要每60分钟重新授权您的应用程序的情况下续订会话,您需要在应用程序将调用的某个位置运行服务器端脚本.然后,服务器端脚本与Spotify服务器通信,以续订或交换新令牌.

ios-sdk下载中的演示项目目录包含一个示例脚本,您可以在本地使用该脚本进行开发.

一旦你有了它,这很容易.在某些地方,您将拥有一些配置交换/刷新URL的代码:

let auth = SPTAuth.defaultInstance()
auth.clientID = Constant.SPOTIFY_CLIENT_ID;
auth.redirectURL = Constant.SPOTIFY_AUTH_CALLBACK_URL
auth.tokenSwapURL = Constant.SPOTIFY_TOKEN_SWAP_URL
auth.tokenRefreshURL = Constant.SPOTIFY_TOKEN_REFRESH_URL
auth.sessionUserDefaultsKey = Constant.SPOTIFY_SESSION_USER_DEFAULTS_KEY
Run Code Online (Sandbox Code Playgroud)

然后,当您想要登录或续订会话时,您可以使用以下内容:

func loginOrRenewSession(handler: (loginTriggered: Bool, error: NSError?) -> Void) {
    guard auth.session != nil else {
        print("will trigger login")
        UIApplication.sharedApplication().openURL(auth.loginURL)
        handler(loginTriggered: true, error: nil)
        return
    }

    if auth.session.isValid() {
        print("already have a valid session, nothing to do")
        handler(loginTriggered: false, error: nil)
        return
    }

    print("will renew session")
    auth.renewSession(auth.session) { error, session in
        self.auth.session = session            
        handler(loginTriggered: false, error: error)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 究竟.服务器只是作用的认证使者 - 接受从iOS客户端的请求,将其转交Spotify的服务器,用你的Spotify客户端密钥+密认证,并返回来自服务器的响应(加密令牌之后).它是这样完成的,因此您无需在iOS应用程序中包含Spotify客户端密钥. (3认同)