This is a simple script I wrote to migrate my Spotify playlists to YouTube music one at a time. Here's how to use it.
Note: Following 4 step instructions are for playlist_migration.py script that uses YouTube OAuth token. There is a version of this script named playlist_migration_cookie.py that takes Auhorization and Cookie headers from logged in YouTube music session to function.
For YouTube API, signup/login to Google Cloud console and then create an API key and OAuth2.0 client ID, as well as OAuth consent screen. Make sure you give at least YouTube Data API V3 scope to your API key/client
For Spotify, sign up/in for Spotify for Developers site and create an API key there.
For both of these, you would need 3 things:
- API Key
- API Secret
- Client ID
- Go to
https://accounts.google.com/o/oauth2/v2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&access_type=offline&include_granted_scopes=true&state=state_parameter_passthrough_value&redirect_uri=http%3A%2F%2Flocalhost&response_type=code&client_id=<YOUR_ACTUAL_CLIENT_ID> - After approving the data access, it would redirect you to a localhost URL like
http://localhost/?state=state_parameter_passthrough_value&code=<SUPER_SECRET_CODE_THAT_GOOGLE_GAVE_YOU>&scope=https://www.googleapis.com/auth/youtubeRemember: You would need to addhttp://localhostto your API key's redirect URL from Google Cloud console. - Use the
codefrom previous step to make an HTTP call like:curl --data "code=<SECRET_CODE_GOES_HERE>&client_id=<CLIENT_ID>&client_secret=<ULTRA_SECRET_API_SECRET>&redirect_uri=http://localhost&grant_type=authorization_code" https://oauth2.googleapis.com/tokenThat should give youaccess_tokenwhich you can use to make API calls for about an hour. It will also give you arefresh_token, keep it safe. You can use it to refresh youraccess_tokenwhen it expires. - Refresh the
access_tokenusing:curl --data "client_id=<YOUR CLIENT ID>&client_secret=<YOUR API SECRET>&grant_type=refresh_token&refresh_token=<REFRESH_TOKEN_FROM_THE_PREVIOUS_STEP>" https://oauth2.googleapis.com/token
- Go to
https://accounts.spotify.com/authorize?response_type=code&client_id=<YOUR SPOTIFY CLIENT ID>&scope=playlist-read-private&redirect_uri=http://localhost - After authenticating the app, you'd be redirected to localhost endpoint like:
http://localhost/?code=<VERY SECRET CODE> - Create a Basic auth token like:
echo -n 'client_id:client_secret' | base64(discard any newline in this output. Your token is a string in a single line) and make a POST call like:url -X POST --data 'code=<SECRET CODE FROM PREVIOUS STEP>&grant_type=authorization_code&redirect_uri=http://localhost' -H 'Authorization: Basic <TOKEN CREATED USING YOUR API KEY'S CLIENT_ID & CLIENT_SECRET>' -H 'Content-Type: application/x-www-form-urlencoded' https://accounts.spotify.com/api/token - This should give you access_token and refresh_token to use.
- You can refresh the token using:
curl -X POST --data "grant_type=refresh_token&refresh_token=<YOUR REFRESH TOKEN FROM PREVIOUS STEP>" -H "Authorization: Basic <YOUR BASIC TOKEN FROM PREVIOUS STEP>" -H "Content-Type: application/x-www-form-urlencoded" https://accounts.spotify.com/api/token
- For Spotify, you can fetch playlists using an API calls. But it's easier to simply open the Spotify web client and extract it from the URL like
https://open.spotify.com/playlist/3IIwBZPVmBrva1zszVRZ5Iwhere3IIwBZPVmBrva1zszVRZ5Iis the playlist ID. - Similarly, you need to first create a playlist on YouTube music and open it in the web player. However, when you open a playlist page
https://music.youtube.com/browse/VLPLcvO-lt9W-7Noyp5HNztvkanrgPQ3m7mM, the playlist ID isPLcvO-lt9W-7Noyp5HNztvkanrgPQ3m7mMi.e. remove ignoreVPin the playlist string. Don't know why, ask Google.
python3.12 playlist_migration.py <spotify_token> <youtube_token> <spotify_playlist> <youtube_playlist>. It would ask you to select a YouTube search result for each song of the playlist. Just type the number and hit enter. If you want to select the first one, skip entering a number, just hit enter. 😎
Important note: Google restricts API key usage unless you pay them or get increased quota. It's 10,000 points per day, and WRITE action takes 50 points, so you can add up to 200 songs to your playlists like this per day.
If you want to use playlist_migration_cookie.py script, log into YouTube music on the browser, hit F12 to open developser console and look for POST calls that has Authorization and Cookie request headers. There should be some https://music.youtube.com/youtubei/v1/player/heartbeat calls, too. Take Cookie and Authorization strings and use that for this script.