简体   繁体   中英

Play songs in Spotify from code in terminal Python

Hi everyone I'm seeing tutorials of Spotify API but I have one doubt, it's possible to send directly from code comands like play, next or play an specific song?

And if you are playing spotify in your phone it will change the song?

Update 1: This is the code that I have at the moment but I have this error message when I run it: (HELP WITH THIS)

SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/me/player/play:
 Player command failed: Premium required, reason: PREMIUM_REQUIRED

And this is my code:

from spotipy.oauth2 import SpotifyClientCredentials
import spotipy


client_id = ""
client_secret = ""
autor = 'Radiohead' 

sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id, client_secret))
result = sp.search(autor)
print(result)
#
sp.start_playback(uris=['spotify:artist:4Z8W4fKeB5YxbusRsdQVPb'])

Here is working code:

import spotipy
from spotipy.oauth2 import SpotifyOAuth
from pprint import pprint

client_id = ""
client_secret = ""
redirect_uri = ""
scope = "user-read-playback-state,user-modify-playback-state"

sp = spotipy.Spotify(
        auth_manager=spotipy.SpotifyOAuth(
          client_id=client_id,
          client_secret=client_secret,
          redirect_uri=redirect_uri,    
          scope=scope, open_browser=False))

# Shows playing devices
res = sp.devices()
pprint(res)

# Change track
sp.start_playback(uris=['spotify:track:6gdLoMygLsgktydTQ71b15'])

You have to put a dummy redirect URL inside the code and in the dashboard of your app.
The script will ask “Go to the following URL:”, and after logging in, you'll need copy the resulting URL after “Enter the URL you was redirected to:”.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM