通过终端中的简单菜单选择此选项时,我尝试设置启动spotify播放的脚本。
问题是我希望这个脚本是动态的,我需要输入一个用户名,当在spotify web autorization页面上重定向时,请求登录和密码,而不是要求接受python脚本。
现在我有了这段代码:
import sys
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import json
import os
import spotipy.util as util
from json.decoder import JSONDecodeError
os.environ['SPOTIPY_CLIENT_ID'] = 'b0g0000Myclientid000000'
os.environ['SPOTIPY_CLIENT_SECRET'] = 'b0g0000Myclientsecret000000'
os.environ['SPOTIPY_REDIRECT_URI'] = 'https://google.com/'
# Get the spotify username from the terminal
username = sys.argv[1]
scope = 'user-read-private user-read-email user-read-playback-state user-modify-playback-state'
try:
token = util.prompt_for_user_token(username, scope)
except:
os.remove(f".cache-{username}")
token = util.prompt_for_user_token(username, scope)
# Creation of the spotify object
sp = spotipy.Spotify(auth=token)
user = sp.current_user()
try:
devices = sp.devices()
deviceID = devices['devices'][0]['id']
except:
print("No devices found...")
print(json.dumps(devices, sort_keys=True, indent=4))
playback = sp.current_user_playing_track()
if playback != None:
current_artist = playback['item']['artists'][0]['name']
current_track_name = playback['item']['name']
print('************** Currently playing : ************** \n Artist: ' + current_artist + '\n Track name: ' + current_track_name)
track = sp.search(playback, 1, 0, 'track')
# print(json.dumps(track, sort_keys=True, indent=4))
# For testing purposes : print(json.dumps(playback, sort_keys=True, indent=4))
while True:
print("\n\nWhat to do ?\n- Play spotify [0]:\n- Play a specific song [1]:\n- Quit [q]")
menu_answer = input()
if menu_answer == '0':
is_playing = playback['is_playing']
if is_playing == False:
sp.start_playback()
print("Started playing the playback (Current song: " + current_track_name + " by " + current_artist + ")")
else:
print("Already playing ;)")
elif menu_answer == '1':
print("we will play a song")
elif menu_answer == 'q':
break
else:
pass
非常感谢!
答案 0 :(得分:0)
无法实现OAuth流程来授权用户,而不向他们显示授权您的脚本的屏幕。这需要为任何新用户完成。
但是,您可以保存用户的凭据,并使用这些凭据获取已授权您的脚本的人员的授权详细信息。
E.g。
如果我误解了这个问题,请告诉我。