2017-05-30 142 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中配置並在我的當前應用程序中使用它。