Search code examples
pythonoauthspotify

Pass OAuth spotify token to cron job using spotipy


I have a python script that adds tracks to my Spotify playlist using spotipy (https://github.com/plamere/spotipy). When I initially run the script it gives me a url to enter into my browser, which in turn redirects me to another url. I then paste this redirected url into the command line, which authorizes my script. I can keep running this script and it does not ask me to re-verify since it refreshes the token. All of this works fine until I attempt to run the script as a cron job. Viewing the output from the cron job, I can see that it is asking for the redirected url because the token I received when I manually ran the script is not shared with the cron job. Is there a way to either share this token with the cron job and have it refresh or somehow automate the verification process and send spotipy the redirected url? I have attempted manually assigning the token I first received to the token variable in my script but this only works for an hour, after which it stops working because I need a new token.


Solution

  • The token is in the file .cache-USERNAME so you should be able to use that file for your cronjob I believe. Put it in the same folder.