Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spotify API Authorization for cron job

I'm creating a node.js application that will update playlists (owned by an account in which I have credentials) daily. According to the Spotify documentation, to add tracks to a playlist (https://developer.spotify.com/web-api/add-tracks-to-playlist/), authorization must be supplied using oauth2.

I'm struggling to find a way to do this completely server side with no redirects/etc. It seems like if I can get a refresh token, I can just use that?

I've been looking at the spotify web api node module (https://github.com/thelinmichael/spotify-web-api-node), oauth.io, and the spotify api.

Any ideas would be appreciated! There is only one account that will have to be authenticated, so it could be hard-coded at least for now.

like image 297
Becky Siegel Avatar asked Jul 07 '15 23:07

Becky Siegel


People also ask

How do I get authorization token for Spotify API?

Request Access Token If the user accepted your request, then your app is ready to exchange the authorization code for an Access Token. It can do this by making a POST request to the /api/token endpoint. This field must contain the value "authorization_code" . The authorization code returned from the previous request.

Does Spotify use OAuth?

If you're interested in working with the Spotify API, you'll need an OAuth token in order to access most of its data. In this blog post, we'll explain what OAuth is, how it works, and break down each step in the context of Spotify's OAuth flow.

Is Spotify API free?

You can make similar calls through the Web API to retrieve information from the Spotify catalog about artists, tracks and playlists. There is a huge amount of data available, and the best part is that it's free to access.

What is access token in Spotify API?

The access token allows you to make requests to the Spotify Web API. To do so, you need to include the following header in your API calls: HEADER PARAMETER. VALUE. Authorization.


1 Answers

You've picked the correct authorization flow - Authorization Code, since you need an access token that's connected to the user who owns the playlists you're updating. This of course also gives you the ability to refresh the token whenever you need to. (The expiration time is one hour, but you don't need to refresh the access token until your application actually needs to use it.)

As a sidenote, the Client Credentials flow is meant for server to server communication that doesn't require a user's permission, e.g. to search, read a playlist, or retrieve new releases. The Implicit Grant flow is meant to be used in frontends, and doesn't allow you to refresh the token.

I'm struggling to find a way to do this completely server side with no redirects/etc. It seems like if I can get a refresh token, I can just use that?

Once you have the refresh token you can continue to use it to retrieve new access tokens, which can be done without any user interaction. You need to do some preparation work to retrieve the refresh token though.

Following the steps describing the Authorization Code flow, you first need to direct the playlist's owner to a URL on Spotify's account server.

The documentation contains the following example URL:

GET https://accounts.spotify.com/authorize/?client_id=5fe01282e44241328a84e7c5cc169165&response_type=code&redirect_uri=https%3A%2F%2Fexample.com%2Fcallback&scope=user-read-private%20user-read-email&state=34fFs29kd09

Simply replace the client_id and redirect_uri with your application's information. Also modify the scope parameter to match the scopes you need, which from my understanding of your use case is playlist-read-private,playlist-modify-private,playlist-read-collaborative since you want to be able to read and modify all of the user's playlists. Supplying state is not required.

Using spotify-web-api-node you can generate this URL using the createAuthorizeURL method, but since you're only doing this once it's unnecessary to write code for it.

Instead, simply open the URL in your browser.

If done successfully, you'll be taken through a little login dance where your application asks for your permission to read and modify your playlists. When this is completed, Spotify's account service will redirect the browser to your redirect_uri URL with a code query parameter included as described in step 3 in the Authorization Guide.

However, since you're only doing this once, it would be enough to start a webserver on your own machine, set your application's redirect_uri to your localhost, and complete the login flow. Have a look at web-api-auth-examples for a ready-made node.js application that fires up an express server and reads the authorization code.

Once you've got the code, you can trade it for an access token using cURL as it's done in step #4 in the Authorization Guide, or use the code in the web-api-auth-examples repository.

Finally, with the tokens retrieved (step #5), you can start to use the Web API with the access token, and get a new one when it expires using the request in step #7.

spotify-web-api-node has a helper method to refresh the token. Search the main documentation for the refreshAccessToken method.

like image 59
Michael Thelin Avatar answered Sep 19 '22 13:09

Michael Thelin