Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AppRemote failing to authenticate #261

Closed
vapor-pawelw opened this issue Jun 15, 2020 · 1 comment
Closed

AppRemote failing to authenticate #261

vapor-pawelw opened this issue Jun 15, 2020 · 1 comment

Comments

@vapor-pawelw
Copy link

vapor-pawelw commented Jun 15, 2020

Hello, this is my example class for connecting

import Foundation

final class SpotifyTestManager: NSObject {
    static let shared = SpotifyTestManager()
    
    enum Config {
        static var clientID: String { /* Client ID string returned here */ }
        static var redirectURL: URL { /* url returned here */ }
        static var tokenSwapURL: URL { /* url returned here */ }
        static var tokenRefreshURL: URL { /* url returned here */ }
    }
    
    lazy var player: SPTAppRemote = {
        let remote = SPTAppRemote(
            configuration: config,
            logLevel: .debug
        )
        
        remote.delegate = self
        return remote
    }()
    
    private lazy var config: SPTConfiguration = {
        let config = SPTConfiguration(
            clientID: Config.clientID,
            redirectURL: Config.redirectURL
        )
        config.tokenSwapURL = Config.tokenSwapURL
        config.tokenRefreshURL = Config.tokenRefreshURL
        return config
    }()
    
    private lazy var sessionManager: SPTSessionManager = {
        return SPTSessionManager(configuration: config, delegate: self)
    }()
    
    override init() {
        super.init()
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(willResignActive),
                                               name: UIApplication.willResignActiveNotification,
                                               object: nil)
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(didBecomeActive),
                                               name: UIApplication.didBecomeActiveNotification,
                                               object: nil)
    }
    
    func connect() {
        let scopes: SPTScope = [.streaming, .playlistReadPrivate, .userLibraryRead, .userReadPrivate, .userModifyPlaybackState]
        sessionManager.initiateSession(with: scopes, options: .clientOnly)
    }
    
    func toggle() {
        
    }
    
    func next() {
        
    }
    
    @discardableResult
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        let willParse = sessionManager.application(app, open: url, options: options)
        
        print("BACK: \(willParse)")
        
        return willParse
    }
    
    @objc private func willResignActive() {
        if player.isConnected {
            player.disconnect()
        }
    }
    
    @objc private func didBecomeActive() {
        if let token = player.connectionParameters.accessToken, sessionManager.session != nil {
            player.connect()
        }
    }
}

extension SpotifyTestManager: SPTSessionManagerDelegate {
    func sessionManager(manager: SPTSessionManager, didInitiate session: SPTSession) {
        log.verbose("INITIATE")
        player.connectionParameters.accessToken = session.accessToken
        
        SPTAppRemote.checkIfSpotifyAppIsActive { isActive in
            if isActive {
                self.player.connect()
            } else {
                self.player.authorizeAndPlayURI("")
            }
        }
    }
    
    func sessionManager(manager: SPTSessionManager, didFailWith error: Error) {
        print("FAIL")
    }
    
    func sessionManager(manager: SPTSessionManager, didRenew session: SPTSession) {
        print("RENEW")
    }
    
    func sessionManager(manager: SPTSessionManager, shouldRequestAccessTokenWith code: String) -> Bool {
        print("REQ CODE: \(code)")
        return true
    }
}

extension SpotifyTestManager: SPTAppRemoteDelegate {
    func appRemoteDidEstablishConnection(_ appRemote: SPTAppRemote) {
        print("PLAYER-ESTABLISH")
    }
    
    func appRemote(_ appRemote: SPTAppRemote, didFailConnectionAttemptWithError error: Error?) {
        print("PLAYER-FAIL")
    }
    
    func appRemote(_ appRemote: SPTAppRemote, didDisconnectWithError error: Error?) {
        print("PLAYER-DISCONNECT")
    }
}

I'm always lading on "INITIATE" then "PLAYER-FAIL".
This is showing up in the console:

AppRemote: Connecting...
AppRemote: Established a connection to the Spotify app.
AppRemote: Failed to authenticate with the Spotify app.
AppRemote: Failed to establish a sesssion with error: Error Domain=com.spotify.app-remote.wamp-client Code=-1001 "wamp.error.not_authorized" UserInfo={details={
    message = "Token not valid.";
}, NSLocalizedFailureReason=wamp.error.not_authorized}

The Spotify app (8.5.60) is playing in the background all the time, and it does an app-switch, but always fails on setting up the remote. I also have the Info.plist set up for redirect host (and it is properly returning to the app after going through Spotify).

I've also seen this thread and the error looks similar (#125)

Any help regarding this?

@vapor-pawelw
Copy link
Author

It was due to no .appRemoteControl scope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant