【问题标题】:Spotify session managementSpotify 会话管理
【发布时间】:2016-06-27 18:13:12
【问题描述】:

我在我的应用中有一个 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)
    }
}

检查会话是否存在

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

保存会话

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

加载会话

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
    }
}

更新会话 - 在应用启动时调用

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 返回零。 我看到了一些关于refreshToken的信息,但我不知道在哪里可以看到它。

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

【问题讨论】:

    标签: ios swift spotify


    【解决方案1】:

    为了在用户无需每 60 分钟重新授权您的应用程序的情况下更新会话,您需要在某个地方运行您的应用程序将调用的服务器端脚本。然后,服务器端脚本与 Spotify 服务器对话以更新或换出新的令牌。

    ios-sdk 下载中的demo projects 目录包含a sample script,可以在本地用于开发。

    一旦你有了它,它就很容易了。在某个地方你会有一些代码来配置你的交换/刷新 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
    

    然后,当你想登录或更新会话时,你可以有这样的东西:

    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)
        }
    }
    

    【讨论】:

    • 据我了解,我需要在我的服务器上实现交换和刷新功能,并将这些 url 添加到 SPTAuth()。 Spotify 会使用我的后端自动更新我的令牌吗?
    • 我不会自动说 - 您仍然需要定期检查您是否拥有有效的令牌并在需要时刷新它。令牌在 60 分钟后过期。
    • 如果我错了,请纠正我:我在服务器上添加交换和更新功能,并将它们的 URL 添加到 STPAuth()。登录后,我会收到有效期为 1 小时的 accessToken。如果我关闭我的客户端并在 2 小时后再次打开它并调用“loginOrRenewSession”会话将恢复吗?还是我需要定期为服务器上的所有用户手动更新访问令牌?
    • 没错。服务器只是充当经过认证的信使 - 接受来自 iOS 客户端的请求,将它们传输到 Spotify 服务器,使用您的 Spotify 客户端密钥 + 秘密进行身份验证,并从服务器返回响应(在加密令牌之后)。这样做是为了让您无需在 iOS 应用中包含您的 Spotify 客户端密钥。
    • @brki 你在用SPTAuth.loginURL 来获取loginURL 吗?
    猜你喜欢
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 2012-10-11
    • 2011-04-29
    • 2011-10-22
    • 2011-08-01
    • 2012-11-12
    • 2014-06-19
    相关资源
    最近更新 更多