قالب وردپرس درنا توس
Home / IOS Development / ios – Spotify SessionManager continuously fails with "invalid_grant" error

ios – Spotify SessionManager continuously fails with "invalid_grant" error



What I'm trying to do:
I'm implementing the Spotify SDK in my iOS project. I receive successful access signs for Spotify's API, as I can do things like search for artists, search songs, and view playlists using said API.

All I struggle to do is play music with SDK. I have a button that, when I click, wants the following flow to occur:

I request Spotify to access by doing the following function and using the following session processing:

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

lat var configuration = SPTCconfiguration (
clientID: SpotifyClientID,
redirectURL: SpotifyRedirectURL
)

lat 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") {
config.tokenSwapURL = tokenSwapURL
config.tokenRefreshURL = tokenRefreshURL
config.playURI = ""
}
let manager = SPTSessionManager (configuration: configuration, delegate: self)
return conductor
} ()

func requestSpotifyAccess () {
let the wishScopes: SPTScope = [.appRemoteControl, .userReadPrivate]
self.sessionManager.initiateSession (with: demandScopes, alternatives: .default)
}

After initiating an SPTS session, I will connect to the remote:

  lat was 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 ()
}

Upon app connection, I want to play the ID of a Spotify track declared globally:

  was pendingSpotifyId: String!

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

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

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

My problem:
This flow is broken up as each time I try to initiate a session, sessionManager (manager: SPTSessionManager, didFailMed error: Error) is always called to return the following error:
Error Domain = com.spotify.sdk.login Code = 1

"invalid_grant" UserInfo = {NSLocalizedDescription = invalid_grant}

I need the session to start successfully so that sessionManager (manager: SPTSessionManager, didInitiate session: SPTSession) can be dialed and I can connect to my remote and finally play my Spotify track.

What I've tried:
I've secured a number of things:

  1. Make sure the status of the Spotify app in the background of the user's device is playing (per this ticket: https://github.com/spotify/ios-sdk/issues/31)
  2. Ensure that the correct scope is in place when you receive an access token. Returned JSON looks like:

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

Things I'm suspicious of:
I don't know if my token switch via Heroku is done correctly. This is the only reason I can think of why I should have this problem. If I can use the Spotify API, is this proof enough that my token swap is done correctly? (I suspect it is)


Source link