2017-05-30 138 views
2

Spotify iOS SDK全部位于objective-c中,截至今天,它看起来像使用SDK的任何请求都需要令牌。当我尝试搜索一首歌,我得到这个错误:在Spotify iOS SDK中设置身份验证令牌

["error": { message = "No token provided"; status = 401; }]

这里是我的appDelegate代码:

var auth = SPTAuth() 
var window: UIWindow? 


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    //Neccessary Spotify stuff 
    auth.redirectURL = URL(string: "my_redirect_url") 
    auth.sessionUserDefaultsKey = "current session" 

    FIRApp.configure() 

    window = UIWindow(frame: UIScreen.main.bounds) 
    window?.makeKeyAndVisible() 
    window?.rootViewController = CustomTabBarController() 

    return true 
} 

// 1 
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { 
    // 2- check if app can handle redirect URL 
    if auth.canHandle(auth.redirectURL) { 
     // 3 - handle callback in closure 
     auth.handleAuthCallback(withTriggeredAuthURL: url, callback: { (error, session) in 
      // 4- handle error 
      if error != nil { 
       print("error!") 
      } 
      // 5- Add session to User Defaults 
      let userDefaults = UserDefaults.standard 
      let sessionData = NSKeyedArchiver.archivedData(withRootObject: session as Any) 
      userDefaults.set(sessionData, forKey: "SpotifySession") 
      userDefaults.synchronize() 
      // 6 - Tell notification center login is successful 
      NotificationCenter.default.post(name: Notification.Name(rawValue: "loginSuccessfull"), object: nil) 
     }) 
     return true 
    } 
    return false 
} 

如何设置在斯威夫特令牌?

回答

0
var auth = SPTAuth.defaultInstance()! 
var session:SPTSession! 

所有设置重定向URL &客户端ID首先:

  let redirectURL = "xyz://" // put your redirect URL here 
      auth.redirectURL  = URL(string: redirectURL) 
      auth.clientID  = kClientId 
      auth.requestedScopes = [SPTAuthStreamingScope, SPTAuthPlaylistReadPrivateScope, SPTAuthPlaylistModifyPublicScope, SPTAuthPlaylistModifyPrivateScope] 
      loginUrl = auth.spotifyWebAuthenticationURL() 

然后使用登录方法:

 if UIApplication.shared.openURL(loginUrl!) { 

      if auth.canHandle(auth.redirectURL) { 
       // To do - build in error handling 
      } 
     } 

做让我知道如果你需要为同任何更多的帮助。我在Swift中配置并在我的当前应用程序中使用它。