I have both Apple Music and Spotify accounts, which I try to update and sync regularly. In an attempt to automate the process, I have created two different scripts which both implement a combination of python Spotipy module and iPhone Shortcuts functionality. In order to run the python scripts on my iPhone, I use Pyto application, which comes in handy as it is accessible by Shortcuts.
Whereas the script that converts a given Spotify playlist to an equivalent Apple Music one works fine, I’m facing some problems with its counterpart, which requires further authentication. Basically, the idea behind the script is to extract data of a given Apple Music playlist using Shortcuts, and then pass it as a JSON object to the python script, which handles the Spotify authentication and songs additions.
When running the script on my iPhone, a Spotify is opened in the Safari browser and nothing happens. The following block shows the authentication part in my code:
from spotipy.oauth2 import SpotifyOAuth
import spotipy
CID = 'MY_API_CLIENT_ID'
SECRET = 'MY_API_SECRET'
REDIRECT_URI = "http://localhost:5000"
SCOPE = "playlist-modify-public"
# Initiates a Spotify connection
def sp_auth():
client_credentials_manager = SpotifyOAuth(client_id=CID, client_secret=SECRET, redirect_uri=REDIRECT_URI, scope=SCOPE)
return spotipy.Spotify(auth_manager=client_credentials_manager)
# Main Function
def main():
sp_auth()
# The script is executed
if __name__ == '__main__':
main()
In the process of trying to solve the problem, I dug into the docs of the Spotify authentications process and Spotipy source code. I’ve understood the OAuth protocol authentication flow and ensured the common solution raised by people regarding the redirection uri specified both in the code and in the Spotify Application Dashboard. In addition, changing the default browser in my phone to Chrome didn’t change the outcome.
After that, I tried to run the exact same code on my Windows PC and it worked fine! An authentication was made perfectly and I was able to make changes to my Spotify account.
The fact that the code worked on my computer but not on my iPhone, made me think that there may be either some restrictions regarding creation of local http servers in iOS devices (as done behind the scenes in the Spotipy module), or that there may be a different approach in handling OAuth authentication in Apple appliances. Unfortunately, it’s very complicated to monitor code functioning in iOS devices, related to networking and OS events.
I didn’t find any solution in the web so I hope some of you in the forum will have a nice solution for me 🙂