Spotify SessionManager 不断失败并出现错误 "invalid_grant"

Spotify SessionManager continually failing with error "invalid_grant"

我想做什么: 我正在将 Spotify SDK 实施到我的 iOS 项目中。我已成功收到 Spotify API 的访问令牌,因为我能够使用上述 API.

执行搜索艺术家、搜索歌曲和查看播放列表等操作

我正在努力做的一件事是使用 SDK 播放音乐。我有一个按钮,单击该按钮后,我希望发生以下流程:

我通过执行以下功能并使用以下会话管理器请求 Spotify 访问权限:

let SpotifyClientID = "###"
let SpotifyRedirectURL = URL(string: "bandmate://")!

lazy var configuration = SPTConfiguration(
    clientID: SpotifyClientID,
    redirectURL: SpotifyRedirectURL
)

lazy var sessionManager: SPTSessionManager = {
    if let tokenSwapURL = URL(string: "https://bandmateallcaps.herokuapp.com/api/token"),
        let tokenRefreshURL = URL(string: "https://bandmateallcaps.herokuapp.com/api/refresh_token") {
        configuration.tokenSwapURL = tokenSwapURL
        configuration.tokenRefreshURL = tokenRefreshURL
        configuration.playURI = ""
    }
    let manager = SPTSessionManager(configuration: configuration, delegate: self)
    return manager
}()

func requestSpotifyAccess() {
    let requestedScopes: SPTScope = [.appRemoteControl, .userReadPrivate]
    self.sessionManager.initiateSession(with: requestedScopes, options: .default)
}

启动 SPTSession 后,我想连接我的遥控器:

lazy var appRemote: SPTAppRemote = {
    let appRemote = SPTAppRemote(configuration: configuration, logLevel: .debug)
    appRemote.delegate = self
    return appRemote
}()

func sessionManager(manager: SPTSessionManager, didInitiate session: SPTSession) {
    self.appRemote.connectionParameters.accessToken = session.accessToken
    self.appRemote.connect()
}

应用程序连接后,我想播放全局声明的 Spotify 曲目 ID:

var pendingSpotifyId: String!

func appRemoteDidEstablishConnection(_ appRemote: SPTAppRemote) {
    print("connected")

    self.appRemote.playerAPI!.delegate = self
    self.appRemote.playerAPI!.subscribe(toPlayerState: { (result, error) in

        if let error = error {
            debugPrint(error.localizedDescription)
        } else if self.pendingSpotifyId != nil {
            self.appRemote.playerAPI!.play(self.pendingSpotifyId, callback: { (any, err) in
                self.pendingSpotifyId = nil
            })
        }
    })
}

我的问题: 每当我尝试启动会话时,此流程就会中断,总是调用 sessionManager(manager: SPTSessionManager, didFailWith error: Error) 并返回以下错误: Error Domain=com.spotify.sdk.login Code=1 "invalid_grant" UserInfo={NSLocalizedDescription=invalid_grant}

我需要会话成功启动,以便 sessionManager(manager: SPTSessionManager, didInitiate session: SPTSession) 可以被调用,我可以连接我的遥控器,最终播放我的 Spotify 曲目。

我尝试过的: 我已经确保了很多事情:

  1. 确保用户设备后台的 Spotify 应用程序状态正在播放(根据此票证:https://github.com/spotify/ios-sdk/issues/31
  2. 确保在接收访问令牌时具有正确的范围。返回的 JSON 看起来像:

    {"access_token":"###","token_type":"Bearer","expires_in":3600,"refresh_token":"###","scope":"app-remote-control user-read-private"}

我怀疑的事情: 我不知道我通过 Heroku 进行的令牌交换是否正确完成。这是我能想到为什么我会遇到这个问题的唯一原因。如果我能够使用 Spotify API,这个证据是否足以证明我的代币交换是正确完成的? (我怀疑是)

以下是我们的发现,希望对您有所帮助:

  1. SpotifySDK 教程没有提到 Bundle IDApp Callback URL 必须在 App Info.plist、源代码、Spotify App Dashboard 和 Heroku Env Vars 之间精确匹配。使用的 Bundle ID 必须与您的应用程序 Bundle ID 匹配。
  2. App Callback URL不能有空路径,即:my-callback-scheme://spotify-login-callback
  3. 在应用程序中同时使用 Web Spotify SDK 和 iOS Framework Spotify SDK 时,请注意只有其中一个执行身份验证。否则 App Callback URL 将被调用两次导致错误。
  4. Spotify configuration.playURI 可能需要设置为空字符串而不是 nil。示例应用程序上有注释。
  5. 最好在应用程序中只有一个管理 Spotify 身份验证的对象实例。否则确保从 AppDelegate open url 方法调用正确的对象可能会很棘手。