How to properly login to spotify? - ios

I have an application that allow user to stream song from spotify. I'm using swift here. I'm trying to allow user to login with spotify account, but I got nothing in return. My app did open safari in order to open spotify login page. I've done login but still returns nothing.
I've make sure that my callbackuri is exactly the same as in my spotify dev page, my url schemes is the first section before colon of my callback uri, and my otherlinker is filled with -ObjC
Here is what I've tried
login action:
let spotifyAuth = SPTAuth.defaultInstance()
spotifyAuth.clientID = kSpotifyClientID
spotifyAuth.redirectURL = NSURL(string: kSpotifyCallBackUrl)
spotifyAuth.requestedScopes = [SPTAuthStreamingScope]
let spotifyLoginUrl : NSURL = spotifyAuth.loginURL
UIApplication.sharedApplication().openURL(spotifyLoginUrl)
my appdelegate:
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
}
and my 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")
}
So, Am I missing something?
Any suggestion would be great

make sure your callback uri is all lowercase letters, I had issues with that.

Related

I can't play music with Spotify iOS SDK with Swift 4.0

I have a struggle with Spotify SDK, I followed every step correctly, but I can't play music with my premium account on my project. There is no error or crash, my app directs me to the Spotify login page and after facebook login, It brings me back to my app again. Yesterday I'd get "logged in" print but today I can't. I'm trying to play a song after login and also manually as you see below. I'm wondering, am I lucky to find an answer?
override func viewDidLoad() {
super.viewDidLoad()
self.spotify()
NotificationCenter.default.addObserver(self, selector: #selector(GeneralNewsViewController.updateAfterFirstLogin), name: NSNotification.Name(rawValue: "SpotifySession"), object: nil)
}
func spotify() {
// insert redirect your url and client ID below
auth.redirectURL = URL(string: "correctCallbackURl")
auth.clientID = "correctClientID"
auth.requestedScopes = [SPTAuthStreamingScope, SPTAuthPlaylistReadPrivateScope, SPTAuthPlaylistModifyPublicScope, SPTAuthPlaylistModifyPrivateScope]
loginUrl = auth.spotifyWebAuthenticationURL()
}
func initializaPlayer(authSession:SPTSession){
if self.player == nil {
self.player = SPTAudioStreamingController.sharedInstance()
self.player!.playbackDelegate = self
self.player!.delegate = self
try! player?.start(withClientId: auth.clientID)
self.player!.login(withAccessToken: authSession.accessToken)
}
}
#objc func updateAfterFirstLogin () {
loginButton.isHidden = true
let userDefaults = UserDefaults.standard
if let sessionObj:AnyObject = userDefaults.object(forKey: "SpotifySession") as AnyObject? {
let sessionDataObj = sessionObj as! Data
let firstTimeSession = NSKeyedUnarchiver.unarchiveObject(with: sessionDataObj) as! SPTSession
self.session = firstTimeSession
initializaPlayer(authSession: session)
self.loginButton.isHidden = true
// self.loadingLabel.isHidden = false
}
}
func audioStreamingDidLogin(_ audioStreaming: SPTAudioStreamingController!) {
// after a user authenticates a session, the SPTAudioStreamingController is then initialized and this method called
print("logged in")
self.player?.playSpotifyURI("spotify:track:4aDLPXlzHZm26GppvRwms8", startingWith: 0, startingWithPosition: 0, callback: { (error) in
if (error != nil) {
print("playing!")
} else {
print(error?.localizedDescription)
}
})
}
#objc func play() {
player?.playSpotifyURI("spotify:track:4aDLPXlzHZm26GppvRwms8", startingWith: 0, startingWithPosition: 0, callback: { (err) in
if err != nil {
print(err?.localizedDescription)
} else {
}
})
}
class AppDelegate: UIResponder, UIApplicationDelegate ,UNUserNotificationCenterDelegate{
auth.redirectURL = URL(string: "correctCallbackURl")
auth.sessionUserDefaultsKey = "current session"}
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!)
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
}}
It looks like you're subscribed to the wrong notification, you're subscribing to "SpotifySession" but posting "loginSuccessfull"
this answer will help you making this kind of mistake in the future by having enums for events in a simple way.
good debugging is also key, you could've probably solved this problem with some breakpoints to see where you went wrong.
cheers

Callback in ios not returning for authentication

I'm using swifter to grab the access token
swifter.authorize(with: URL(string: "callback://")!, presentFrom: self, success: { accessToken, response in
print("HELLO")
print(accessToken)
// ...
}, failure: { error in
// ...
})
I can log in successfully because the app opens up SFSafariViewController, but then I'm not directed back to my app, and the success callback is never being called
here's the code for swifter, I see that presentFrom is suppose to be SFSafariViewController, but the delgate methods for SFSafariViewController aren't being triggered
public func authorize(with callbackURL: URL, presentFrom presentingViewController: UIViewController? , success: TokenSuccessHandler?, failure: FailureHandler? = nil) {
self.postOAuthRequestToken(with: callbackURL, success: { token, response in
var requestToken = token!
NotificationCenter.default.addObserver(forName: .SwifterCallbackNotification, object: nil, queue: .main) { notification in
NotificationCenter.default.removeObserver(self)
presentingViewController?.presentedViewController?.dismiss(animated: true, completion: nil)
let url = notification.userInfo![CallbackNotification.optionsURLKey] as! URL
let parameters = url.query!.queryStringParameters
requestToken.verifier = parameters["oauth_verifier"]
self.postOAuthAccessToken(with: requestToken, success: { accessToken, response in
self.client.credential = Credential(accessToken: accessToken!)
success?(accessToken!, response)
}, failure: failure)
}
let authorizeURL = URL(string: "oauth/authorize", relativeTo: TwitterURL.oauth.url)
let queryURL = URL(string: authorizeURL!.absoluteString + "?oauth_token=\(token!.key)")!
if #available(iOS 9.0, *) , let delegate = presentingViewController as? SFSafariViewControllerDelegate {
let safariView = SFSafariViewController(url: queryURL)
safariView.delegate = delegate
presentingViewController?.present(safariView, animated: true, completion: nil)
} else {
UIApplication.shared.openURL(queryURL)
}
}, failure: failure)
}
does it just wait and say "redirecting you back to the application" but never actually goes back to application?
You need to add both of these functions to the Appdelegate
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
Swifter.handleOpenURL(url)
return true
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
Swifter.handleOpenURL(url as URL)
return true
}
also try look at this for further assistance: How to Authorize Twitter with Swifter

Swift - AWS Cognito-Facebook Login

I want to use Facebook for my login process with Cognito, and I've followed a lot of AWS documentation and look at tutorials and questions in Stackoverflow, but I've not found a solution for my problem.
When the user opens the app, it will check if the user is logged in using IdentityManager. If not, it will open a new view where the user can sign in using Facebook using Facebook SDK. After that, I stored the token with a custom IdentityProvider as the documentation said (credentialsProvider.logins is deprecated). Everything seems to work fine, but every time I reopen the application, my session isn't restored.
I found out that if I use AWSIdentityManager.defaultIdentityManager().resumeSessionWithCompletionHandler(handler)I restored my session, but in case the user isn't logged in, it doesn't show my custom login screen as expected, but a Safari web view to Facebook.
Here is my code:
AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let identityManager = AWSIdentityManager.defaultIdentityManager()
identityManager.resumeSessionWithCompletionHandler({
(result, error) -> Void in
if !identityManager.loggedIn {
let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SignIn") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
})
return AWSMobileClient.sharedInstance.didFinishLaunching(application, withOptions: launchOptions)
}
SignInViewController.swift
#IBAction func openFacebookLoginScreen(sender: AnyObject) {
FBSDKLoginManager().logInWithReadPermissions(FACEBOOK_PERMISSIONS, fromViewController: self, handler: { (result, error) -> Void in
if error == nil {
let fbLoginResult : FBSDKLoginManagerLoginResult = result
if fbLoginResult.isCancelled {
print("Cancelled")
}
else {
if FBSDKAccessToken.currentAccessToken() != nil {
self.signInFacebook(FBSDKAccessToken.currentAccessToken().tokenString)
self.dismissSignInView()
}
}
}
})
}
func signInFacebook(fbToken: String){
let logins = [AWSIdentityProviderFacebook : fbToken]
let customProviderManager = CustomIdentityProvider(tokens: logins)
let credentialsProvider = AWSCognitoCredentialsProvider(
regionType:.USEast1,
identityPoolId: COGNITO_IDENTITY_POOL_ID,
identityProviderManager: customProviderManager)
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
}
class CustomIdentityProvider: NSObject, AWSIdentityProviderManager {
var tokens : [NSString : NSString]?
init(tokens: [NSString : NSString]) {
self.tokens = tokens
}
#objc func logins() -> AWSTask {
return AWSTask(result: tokens)
}
}
Apparently my problem was that I was calling IdentityManager.loggedIn inside AppDelegate.swift, so I move it to viewDidLoad() on my main view controller.
Also I changed my sign in code to:
#IBAction func openFacebookLoginScreen(_: AnyObject) {
handleLoginWithSignInProvider(AWSFacebookSignInProvider.sharedInstance())
}
func handleLoginWithSignInProvider(signProvider: AWSSignInProvider){
AWSIdentityManager.defaultIdentityManager().loginWithSignInProvider(signProvider) { (result, error) in
if(error == nil){
let logins = [AWSIdentityProviderFacebook : FBSDKAccessToken.currentAccessToken().tokenString!]
let customProviderManager = CustomIdentityProvider(tokens: logins)
let credentialsProvider = AWSCognitoCredentialsProvider(
regionType:.USEast1,
identityPoolId: self.COGNITO_IDENTITY_POOL_ID,
identityProviderManager: customProviderManager)
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
self.dismissSignInView()
}
}
}

Why is the spotify login not working?

I am using the spotify api, and I am getting an error when I try to login. This is the error -canOpenURL: failed for URL: "spotify-action://" - error: "(null)". Below I have included all the necessary code. What am I doing wrong? I am using the latest soptify api version.
sign in
let kClientId = "hidden"
let kCallbackURL = "testapp://callback"
#IBAction func loginspotify(sender: AnyObject) {
let auth = SPTAuth.defaultInstance()
auth.clientID = kClientId
auth.redirectURL = NSURL(string: kCallbackURL)
auth.tokenSwapURL = NSURL(string: kTokenSwapURL)
auth.tokenRefreshURL = NSURL(string: kTokenRefreshServiceURL)
auth.requestedScopes = [SPTAuthUserReadPrivateScope,SPTAuthStreamingScope,SPTAuthPlaylistModifyPrivateScope,SPTAuthUserReadEmailScope,SPTAuthUserLibraryModifyScope,SPTAuthUserLibraryReadScope,SPTAuthPlaylistReadPrivateScope,SPTAuthUserReadPrivateScope]
var loginURL = auth.loginURL
print(loginURL)
UIApplication.sharedApplication().openURL(loginURL)
}
AppDelegate
let kClientId = "hidden"
let kCallbackURL = "testapp://callback"
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
if(SPTAuth.defaultInstance().canHandleURL(url)){
SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURL(url, callback: {(error: NSError!, session: SPTSession!) -> Void in
if(error != nil){
print("AUTHENTICATION ERROR \(error)")
return
}
let userDefaults = NSUserDefaults.standardUserDefaults()
let sessionData = NSKeyedArchiver.archiveRootObject(session, toFile: "sessiondata")
userDefaults.setObject(sessionData, forKey: "SpotifySession")
userDefaults.synchronize()
NSNotificationCenter.defaultCenter().postNotificationName("loginSuccessful", object: nil)
})
}
return true
}
You need a separate Swap Token server running that hashes a shared secret with your Spotify credentials. This is required as part of the RC5 standard.
Here is an example of a swap service:
https://github.com/spotify/ios-sdk/blob/master/Demo%20Projects/spotify_token_swap.rb

Setting tokens in Spotify iOS app disables login callback

I am trying to set up the login for my iOS app using Spotify's SDK. I have the login working, but only without tokens. Once I add these two lines of code:
SPTAuth.defaultInstance().tokenSwapURL = NSURL(string: kTokenSwapURL)
SPTAuth.defaultInstance().tokenRefreshURL = NSURL(string: kTokenRefreshServiceURL)
The login does not work. This is my code for the login.
AppDelegate.swift
let kClientID = "my-client-id"
let kCallbackURL = "my-callback-url"
let kTokenSwapURL = "my-token-swap-url"
let kTokenRefreshServiceURL = "my-token-refresh-url"
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
// Override point for customization after application launch.
SPTAuth.defaultInstance().clientID = kClientID
SPTAuth.defaultInstance().redirectURL = NSURL(string: kCallbackURL)
SPTAuth.defaultInstance().requestedScopes = [SPTAuthStreamingScope, SPTAuthUserReadPrivateScope, SPTAuthPlaylistReadPrivateScope]
SPTAuth.defaultInstance().sessionUserDefaultsKey = "SpotifySession"
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let loginViewController = LoginViewController(nibName: "LogInViewController", bundle: nil)
let navigationController = UINavigationController(rootViewController: loginViewController)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
let authCallback : SPTAuthCallback = { error, session in
// This is the callback that'll be triggered when auth is completed (or fails).
if (error != nil) {
print(error);
return;
}
let userDefaults = NSUserDefaults.standardUserDefaults()
let sessionData = NSKeyedArchiver.archivedDataWithRootObject(session)
userDefaults.setObject(sessionData, forKey: SPTAuth.defaultInstance().sessionUserDefaultsKey)
userDefaults.synchronize()
AuthHandler.sharedHandler.loginWithSession(session)
};
if SPTAuth.defaultInstance().canHandleURL(url) {
SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURL(url, callback:authCallback)
return true
}
return false;
}
LoginViewController.swift
class LoginViewController: UIViewController {
let kClientID = "my-client-id"
let kCallbackURL = "my-callback-url"
let kTokenSwapURL = "my-token-swap-url"
let kTokenRefreshServiceURL = "my-token-refresh-url"
var session: SPTSession!
var logIn: UIButton!
var auth : SPTAuthViewController?
override func viewWillAppear(animated: Bool) {
// set login callback for what happens when session is got
AuthHandler.sharedHandler.setLoginCallback({ success in
if (success) {
self.transitionToPlaylistScreen()
}
})
// if session is still valid, login
let userDefaults = NSUserDefaults.standardUserDefaults()
if let sessionObj:AnyObject = userDefaults.objectForKey("SpotifySession") { // session available
let sessionDataObj = sessionObj as! NSData
let session = NSKeyedUnarchiver.unarchiveObjectWithData(sessionDataObj) as! SPTSession
if !session.isValid() {
SPTAuth.defaultInstance().renewSession(session, callback: { (error:NSError!, renewdSession:SPTSession!) -> Void in
if error == nil {
let sessionData = NSKeyedArchiver.archivedDataWithRootObject(session)
userDefaults.setObject(sessionData, forKey: SPTAuth.defaultInstance().sessionUserDefaultsKey)
userDefaults.synchronize()
self.session = renewdSession
AuthHandler.sharedHandler.loginWithSession(self.session!)
} else {
print(error.localizedDescription)
}
})
} else {
self.session = session
AuthHandler.sharedHandler.loginWithSession(self.session!)
}
}
}
override func viewDidLoad() {
// add observer for login success
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("transitionToPlaylistScreen"), name: "loginSuccess", object: nil)
// code to set up the login button
}
func transitionToPlaylistScreen() {
if (self.auth != nil) {
self.dismissViewControllerAnimated(true, completion: nil)
self.auth = nil
}
let playlistScreen = PlaylistViewController()
let navigation = UINavigationController(rootViewController: playlistScreen)
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(navigation, animated: true, completion: nil)
})
}
func loginToSpotify() {
// if session isn't valid, login within app
dispatch_async(dispatch_get_main_queue(), {
self.auth = SPTAuthViewController.authenticationViewController()
self.auth?.delegate = AuthHandler.sharedHandler
self.auth!.modalPresentationStyle = .OverCurrentContext
self.auth!.modalTransitionStyle = .CrossDissolve
self.modalPresentationStyle = .CurrentContext
self.definesPresentationContext = true
self.auth!.clearCookies({
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(self.auth!, animated: false, completion: nil)
})
})
})
}
}
AuthHandler.swift
class AuthHandler: NSObject, SPTAuthViewDelegate {
static let sharedHandler = AuthHandler()
var session: SPTSession?
var callback: (Bool -> Void)?
func setLoginCallback(callback: (Bool -> Void)) {
self.callback = callback
}
func authenticationViewController(authenticationViewController: SPTAuthViewController!, didFailToLogin error: NSError!) {
if let function = callback {
function(false)
}
}
func authenticationViewController(authenticationViewController: SPTAuthViewController!, didLoginWithSession session: SPTSession!) {
self.loginWithSession(session)
}
func authenticationViewControllerDidCancelLogin(authenticationViewController: SPTAuthViewController!) {
if let function = callback {
function(false)
}
}
func loginWithSession(session: SPTSession) {
self.session = session
SPTAuth.defaultInstance().session = session
if let function = callback {
function(true)
}
}
}
I guess that your backend (swap/refresh) server is not set up properly, because a not working server will cause log in to fail.
I recommend this repository, which you can set up a simple server on heroku.
What I would recomed you to try several things:
Could you please post what is in yours URL Types Identifier and URL Schemes?
I do not know is it important, but in my case Redirect URI and URL Schemes is the same. Redirect URI can be found here under My Applications.
Try to write second app which is opening your app using URL Scheme. If it will not work then here is problem in URL schemas.
As code snapshot is here:
let kCallbackURL = "myWhosampled://"
let url = NSURL(string: kCallbackURL)
UIApplication.sharedApplication().openURL(url!)
When generating files for tokens using spotify_token_swap.rb file you would need to set correct values for this:
CLIENT_ID = "e6695c6d22214e0f832006889566df9c"
CLIENT_SECRET = "29eb02041ba646179a1189dccac112c7"
ENCRYPTION_SECRET = "cFJLyifeUJUBFWdHzVbykfDmPHtLKLGzViHW9aHGmyTLD8hGXC"
CLIENT_CALLBACK_URL = "spotifyiossdkexample://"
AUTH_HEADER = "Basic " + Base64.strict_encode64(CLIENT_ID + ":" + CLIENT_SECRET)
SPOTIFY_ACCOUNTS_ENDPOINT = URI.parse("https://accounts.spotify.com")
set :port, 1234 # The port to bind to.
set :bind, '0.0.0.0' # IP address of the interface to listen on (all)

Resources