[英]Spotipy invalid access token ? scope problem?
我正在做一个简单的脚本来更新播放列表,但即使一切看起来都不错,我也会出错:`
脚本开始,我的 URI 工作并给我一个令牌,打开 spotify 询问我访问权限但在它失败之后:
HTTP Error for PUT to https://api.spotify.com/v1/playlists/6rkyVmrfPEeWkJm01mbhO1 with
Params: {} returned 401 due to Invalid access token
Traceback (most recent call last):
File "C:\Users\xxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\spotipy\client.py", line 269, in _internal_call
response.raise_for_status()
File "C:\Users\xxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url:
https://api.spotify.com/v1/playlists/6rkyVmrfPEeWkJm01mbhO1
Python 代码:
def func():
scope = 'playlist-modify-public'
username = 'myusername'
SPOTIPY_CLIENT_ID = 'xxxxxxxxxxxx'
SPOTIPY_CLIENT_SECRET = 'xxxxxxxxx'
token = util.prompt_for_user_token(username,scope,SPOTIPY_CLIENT_ID,SPOTIPY_CLIENT_SECRET,redirect_uri='https://www.dev.com')
print(token)
sp = spotipy.Spotify(auth="token")
id = 'https://open.spotify.com/playlist/6rkyVmrfPEeWkJm01mbhO1'
playlist_name = 'it works'
playlist_description = 'updated by script'
sp.user_playlist_change_details(username, id, name=playlist_name, description=playlist_description)
func()
这里再试一次:
我得到:
py spotify_bot.py
HTTP Error for PUT to https://api.spotify.com/v1/playlists/6rkyVmrfPEeWkJm01mbhO1 with Params: {} returned 403 due to Not allowed
An exception occurred
这里的代码
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import json
def func():
SCOPE = 'playlist-modify-public'
USER_ID = 'xifeat'
REDIRECT_URI = 'http://localhost:3000/callback'
CLIENT_ID = '6cff431d1dea40f4812460b4721032b0'
CLIENT_SECRET = 'fb0bcffd4b2b41ac84b9b1d7501dbb80'
PLAYLIST_ID = '6rkyVmrfPEeWkJm01mbhO1'
PLAYLIST_NAME = 'test'
DESCRIPTION = 'Updated !'
try:
auth_manager = SpotifyOAuth(
scope=SCOPE,
username=USER_ID,
redirect_uri=REDIRECT_URI,
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET)
spotify = spotipy.Spotify(auth_manager=auth_manager)
spotify.user_playlist_change_details(
user=USER_ID,
playlist_id=PLAYLIST_ID,
name=PLAYLIST_NAME,
description=DESCRIPTION)
except:
print("An exception occurred")
func()
`
你需要在这里传递正确的参数
playlist_id
只是播放列表的id,不是全URL。
def user_playlist_change_details(
self,
user,
playlist_id,
name=None,
public=None,
collaborative=None,
description=None,
):
Parameters:
- user - the id of the user
- playlist_id - the id of the playlist
- name - optional name of the playlist
- public - optional is the playlist public
- collaborative - optional is the playlist collaborative
- description - optional description of the playlist
此代码将起作用
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import json
def func():
SCOPE = 'playlist-modify-public'
USER_ID = 'your-user-id'
REDIRECT_URI = 'your-redirect-uri'
CLIENT_ID = 'your-client-id'
CLIENT_SECRET = 'your-client-secret'
PLAYLIST_ID = 'your-play-list'
PLAYLIST_NAME = 'Oh my new playlist name!'
DESCRIPTION = 'This is new description'
try:
auth_manager = SpotifyOAuth(
scope=SCOPE,
username=USER_ID,
redirect_uri=REDIRECT_URI,
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET)
spotify = spotipy.Spotify(auth_manager=auth_manager)
spotify.user_playlist_change_details(
user=USER_ID,
playlist_id=PLAYLIST_ID,
name=PLAYLIST_NAME,
description=DESCRIPTION)
except:
print("An exception occurred")
func()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.