Step 1: Accessing your Spotify Credentials and Authorizing your App. Log into Spotify’s developer interface, and click the green ‘Create An App’ button. You will be prompted to name. United States of America. Nook HD (Running CyanogenMod) Operating System. My Question or Issue. Hi all, I have been trying to develop a very simple app to be used with the spotify API for android that simply starts a playlist of my choosing when a button is pushed.
Introduction
This post targets beginners and has no code in it. It merely shows all the steps you need to go through from starting with going to the Spotify Developer portal to the final step of getting your Spotify App Client ID and Client Secret. These two string values are the minimum you need to use the Spotify Web Services/endpoints.
Create an Account on the Spotify Developer Portal
In your favourite web browser, go to https://developer.spotify.com/dashboard. If you already are a registered Spotify user, you can use the same account to login to the developer portal. If you have not used Spotify, then you should sign up for a new free account.
Once logged into the Spotify Developer portal, you should see something like the following web page screenshot which is the Spotify Developer Portal Dashboard.
Create a Spotify App in the Spotify Developer Dashboard
Now that you are in the Spotify Developer Dashboard, you can create a Spotify App. To do this, click the 'Create an App' button. In the page fill in the name, you want to give your app and a description. You need to check both checkboxes to agree to the usage terms and conditions. If in doubt or you want to know, you can read the terms and conditions. Then click the 'Create' button. An example screenshot I have filled out for demo purposes is the following:
Now you come back to the Dashboard page and you have your newly created Spotify App. The Client ID is displayed and you can copy this and save it and paste it into your script/code. For the Client Secret, there is an additional step of clicking the link/button to show it. Below is a screenshot of the demo example. I am not going to hide any of this information as I will delete the App and delete the Spotify new account I made for demonstration purposes only during the making of this article. So do not use these values in the screenshots as they will not exist as they are deleted.
So at this point, you have the Spotify API App Client ID and Client Secret string values.
Spotify Web Api Node
Now you need to provide some configuration information if you want to use code to call the Spotify Web Services/endpoints.
Spotify Web Api Token
Configuration Settings
So while the basic needs are met of Spotify App Client ID and Client Secret in order for it to work, you do need to configure settings specific to your script/code calling the Spotify Web Services/endpoints. The following example is a screenshot with filled in information specific to my Blazor Music Client:
You can change your Application Name in the first text field. In the example demo case of this article, it is 'My Spotify App'. For my open-source Blazor Music Client here, any legal name as per Spotify's rules for naming Apps will do. For other purposes, this name is important as this is what will be used and displayed during Authentication and Authorization purposes when connecting to the Spotify Web Services/endpoints.
The Application Description text field I have filled out is an example for demo purposes of this article. Here, anything you want as per the application you are creating can be used.
The Website is the URL of your website which is only important if you are creating something that will go into production. This will be displayed again by the Spotify Authentication and Authorization forms to give the user a chance to know what the application is about before accepting to Authenticate and Authorize it with their Spotify account. For my Blazor Music Client, this can remain empty.
The Redirect URIs field is the URL location of the calling code/script. When you call the Spotify Web Services/endpoints, you need to supply the redirect URI. Here, you have to be very careful as the supplied URI has to be an exact string match for the URI that is calling the Spotify Web Services/endpoints. So even the end/trailing '/' in 'https://localhost:44384/' has to be provided. Without your code hosted on HTTPS, it will not work due to security reasons to protect Spotify Users from harm.
The descriptions for the Bundle IDs and Android Packages is self-evident and specific to smartphone app developers of Apple and Android apps. I am not there yet so you will have to do further research on your own on how to get these pieces of information and fill them in correctly so your Web Service calls from your smartphone apps will work correctly.
When you return to the Spotify Developer portal to change settings or get the Client ID/Client Secret, a different looking page will come. Click the tile button of the Spotify App name. Here is a screenshot:
Conclusion
You now have your Spotify API Client ID and Client Secret with this information. You can use the Spotify API Web Services/endpoints from your script/code.
Thanks again for your patience and time if you have read this far. I wish you luck in your apps using the Spotify API cloud Web Services/endpoints.
History
- 15th August, 2020: Initial version
In this article, we’re sharing tips for connecting Spotify to your mobile iOS application. The guide is based on our web and mobile development experience and explores the stages of SDK connection: the process of login, token authentication, and the built-in Spotify player management.
Spotify offers an application programming interface (API) and a software development kit (SDK).
An API is a collection of routines, data structures, and classes designed specifically to interact with a piece of software. An SDK comprises of the API and tools for its management.
When creating an application, it’s better to use an SDK (if there’s one available), as it simplifies the development. Although the Spotify iOS SDK is in beta (ver. beta-25), it’s stable enough to use. Note that using Spotify’s SDK for commercial projects requires their team’s official written approval.
With Spotify integrated with your app, its users can receive user data, play audio and share public data. The latter includes sharing different Spotify items (albums, artists and playlists), and managing collaborative playlists. However, the ability is limited to public items only.
Implementing the Spotify iOS SDK
For this article, we decided to show you the integration of Spotify’s iOS SDK with a mobile application for listening to music from several audio streaming services. Here, users can link to and undock their Spotify accounts, and log into several devices without re-connecting to the integrated services.
Keep in mind that although there is an official tutorial for implementing Spotify’s SDK, it can only introduce the basic functionality. The process of connecting the SDK to a real product is significantly different in terms of login logic and token management.
First things first, you need to create an XCode project, register a Spotify account and make it premium. It’s necessary for streaming. For those not sure it’s worth the purchase, there’s a free trial.
Registering your app in Spotify
Go to the Spotify’s Dashboard and log into your account. Find a Create an App button there. Enter your name and application description. Then examine the following fields:
Now find this button . Enter your name and app description. After this we are interested in the following fields:
Client ID - your app’s unique identifier, which is required for the SDK integration.
Client Secret - your app’s secret key, which is used on the server.
Redirect URL - a link returning to your application in case of login readdressing to the native app or Safari. Here, we used
projectname://spotify/callback
Bundle ID - your app’s bundle ID.
Creating a SpotifyLoginController
We’re using a custom login, and not the one given in the tutorial because we need to receive a server authentication code.
First, let’s create a controller with UIWebView (go with the UIWebViewController if you wish; we prefer using UIViewController with an additional UIWebView; the delegate is self).
Then, we need to implement a basic Spotify initialization in viewDidLoad:
Spotify Api Data
Here’s a closer look:
SPTAuthStreamingScope, SPTAuthPlaylistReadPrivateScope, SPTAuthPlaylistModifyPublicScope, and SPTAuthPlaylistModifyPrivateScope - are our app’s access rights
- SpotifyRedirectURI is the redirect URL we specified in the Spotify app settings
- SpotifyTokenSwapURL and SpotifyTokenRefreshURLb are something we’re going to discuss in a little while
When the controller appears in viewWillAppear, we need to initialize a login link:
Look at the following parameters:
- client_id - is our app’s client ID
- scope - is the access permission
- redirect_uri - is a redirect URL that lets us return to the app and spot the completion of login
- nosignup = true - disables automatic login
- show_dialog = true - is a required “Is it really you?” user confirmation
- response_type = code - is a type of returned value (you can get code or access token here - we’re interested in the former).
Here’s how the response is processed:
A successful login implementation results into a session, which is available through SPTAuth.defaultInstance().session. The peculiarity of Spotify’s sessions is the duration of their lifespan. At the time of writing, it’s 1 hour long. Once this period expires, you have to call the SPTAuth.defaultInstance().renewSession method.
Seemingly not an issue, you just have to add a SPTAuth.defaultInstance().session.isValid() method validation, but here’s a catch: all sessions are stored locally on devices.
Storing and updating tokens
If you want user to login to several devices without signing into their Spotify over and over again, you’re going to have to do the following:
- Remember we mentioned SpotifyTokenSwapURL and SpotifyTokenRefreshURL? Those are links to the server requests that implement Spotify token updates. When updating a session, you need to fix the renewSession method:
We highly recommend that you reassign swap and refresh links before making any session updates. Otherwise, you might accidentally call renewSession in places, where SPTAuth.defaultInstance() hasn’t been configured yet.
Implement swap and refresh methods on the server. Here’s an example of this process in Ruby.
Once it has been working for an hour or so, call the renewSession method from your SDK, which then queries your server, gets the necessary data, creates a session and stores it on the device (again, locally).
With the following methods, the newly created session can be transformed into a string and back:
A transformed session can be stored either locally or on a server, but you need to encrypt it first.
Songs stream
Spotify SDK allows your app’s users to listen to entire songs without being redirected to the Spotify application.
Audio streaming is done not through the usual AVPlayer, but through the SPTAudioStreamingController. For this example, we saved a Spotify player item as a singleton in the Utils class, which makes it accessible from anywhere:
When launching our app (or once we have a connected Spotify account), we can call the player initialization:
In this example, a Spotify Premium check block (the error code is 9, which means a standard account that doesn’t permit audio streaming) is commented due to request instability (which is plausible given that the SDK is in beta). Streaming is only available when there are no errors, regardless of their type.
The main methods of Spotify’s player
Track listing:
Play and Pause
Stop:
The current position:
The overall track duration:
Rewind:
This concludes our article on implementing Spotify’s iOS SDK for mobile application development. We hope you’ve found something worth learning.
Do your consider integrating Spotify with your mobile application? Contact our sales department today to start working on your project.
Using Spotify Web Api In Mobile App Windows 10
P.S. Do not forget to logout from Spotify