如何正确登录到 spotify?
Posted
技术标签:
【中文标题】如何正确登录到 spotify?【英文标题】:How to properly login to spotify? 【发布时间】:2015-09-28 23:34:41 【问题描述】:我有一个允许用户从 spotify 流式传输歌曲的应用程序。我在这里使用swift。我试图允许用户使用 spotify 帐户登录,但我没有得到任何回报。我的应用确实打开了 safari 以打开 spotify 登录页面。我已经完成登录,但仍然没有返回任何内容。
我已确保我的 callbackuri 与我的 spotify 开发页面中的完全相同,我的 url 方案是我的回调 uri 冒号前的第一部分,并且我的 otherlinker 填充有 -ObjC
这是我尝试过的
登录操作:
let spotifyAuth = SPTAuth.defaultInstance()
spotifyAuth.clientID = kSpotifyClientID
spotifyAuth.redirectURL = NSURL(string: kSpotifyCallBackUrl)
spotifyAuth.requestedScopes = [SPTAuthStreamingScope]
let spotifyLoginUrl : NSURL = spotifyAuth.loginURL
UIApplication.sharedApplication().openURL(spotifyLoginUrl)
我的应用代理:
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool
println("rene jos1")
if (SPTAuth.defaultInstance().canHandleURL(url))
println("rene jos2")
SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURL(url, callback: (error : NSError?, session : SPTSession?) -> Void in
println("rene jos3")
if error != nil
println("Auth error : \(url.description)")
return
let userDefaults = NSUserDefaults.standardUserDefaults()
let sessionData = NSKeyedArchiver.archivedDataWithRootObject(session!)
userDefaults.setObject(sessionData, forKey: "spotifySession")
userDefaults.synchronize()
NSNotificationCenter.defaultCenter().postNotificationName("spotifyLoginSuccesfull", object: nil)
)
return true
if (FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation))
return true
return false
还有我的viewdidload:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateAfterFirstlogin", name: "spotifyLoginSuccesfull", object: nil)
// Do any additional setup after loading the view.
spotifyLoginButton.hidden = true
let userDefaults = NSUserDefaults.standardUserDefaults()
if let sessionObj : AnyObject = NSUserDefaults.standardUserDefaults().objectForKey("spotifySession")
println("rene 2")
let sessionDataObj : NSData = sessionObj as! NSData
let session = NSKeyedUnarchiver.unarchiveObjectWithData(sessionDataObj) as! SPTSession
self.playUsingSession(session)
if !session.isValid()
SPTAuth.defaultInstance().renewSession(session, callback: (error : NSError!, newsession : SPTSession!) -> Void in
let sessionData = NSKeyedArchiver.archivedDataWithRootObject(session)
userDefaults.setObject(sessionData, forKey: "spotifySession")
userDefaults.synchronize()
self.session = newsession
self.playUsingSession(newsession)
println("rene 3")
)
else
println("error refreshing new spotify session")
else
spotifyLoginButton.hidden = false
println("rene 4")
那么,我错过了什么吗? 任何建议都会很棒
【问题讨论】:
它的打印语句是什么? @myles 我得到了 >> 错误 Domain=com.spotify.ios-sdk.playback Code=8 “由于凭据无效,登录 Spotify 失败。” UserInfo=0x7f840bf807b0 NSLocalizedDescription=登录 Spotify 失败,因为凭据无效。 【参考方案1】:确保您的回调 uri 都是小写字母,我对此有疑问。
【讨论】:
以上是关于如何正确登录到 spotify?的主要内容,如果未能解决你的问题,请参考以下文章
如何正确跟踪播放位置? [Android 媒体广播通知 Spotify API]
如何使用 Spotify SDK 和 Swift 3 正确处理令牌刷新。错误代码 = 3840
Spotify PKCE 授权流程返回“code_verifier 不正确”