简体   繁体   中英

Spotipy invalid access token ? scope problem?

Im doing a simple script to update a playlist but I have an error even if everything looks good: `

The script start, I URI works and give me a token, spotify open to asked me the access but after it fail:

   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 code:

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()

here another try:

I get:

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

Here the code

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()

在此处输入图像描述

`

You needs to pass correct parameters in here

The playlist_id just id of playlist, it is not full 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

This code will works

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()

Before change在此处输入图像描述

After change在此处输入图像描述

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