Vignesh R
Vignesh R

Reputation: 53

User authentication for Spotify in Python using Spotipy on AWS

I am currently building a web-app that requires a Spotify user to login using their credentials in order to access their playlists

I'm using the Spotipy python wrapper for Spotify's Web API and generating an access token using,

token = util.prompt_for_user_token(username,scope,client_id,client_secret,redirect_uri)

The code runs without any issues on my local machine. But, when I deploy the web-app on AWS, it does not proceed to the redirected uri and allow for user login.

I have tried transferring the ".cache-username" file via SCP to my AWS machine instance and gotten it to work in limited fashion.

Is there a solution to this issue? I'm fairly new to AWS and hence don't have much to go on or any idea where to look. Any help would be greatly appreciated. Thanks in advance!!

Upvotes: 2

Views: 2203

Answers (1)

Stéphane Bruckert
Stéphane Bruckert

Reputation: 22973

The quick way

  1. Run the script locally so the user can sign in once
  2. In the local project folder, you will find a file .cache-{userid}
  3. Copy this file to your project folder on AWS
  4. It should work

The database way

There is currently an open feature request on Github that suggests to store tokens in a DB. Feel free to subscribe to the issue or to contribute https://github.com/plamere/spotipy/issues/51

It's also possible to write a bit of code to persist new tokens into a DB and then read from it. That's what I'm doing as part of an AWS Lambda using DynamoDB, it's not very nice but it works perfectly https://github.com/resident-archive/resident-archive/blob/a869b73f1f64538343be1604d43693b6165cc58a/functions/to-spotify/main.py#L129..L157


The API way

This is probably the best way, as it allows multiple users to sign in simultaneously. However it is a bit more complex and requires you host a server that's accessible by URL.

This example uses Flask but one could adapt it to Django for example https://github.com/plamere/spotipy/blob/master/examples/app.py

Upvotes: 2

Related Questions