使用 Spotipy 的 invalid_client 错误和授权刷新令牌错误

时间:2021-06-22 05:12:33

标签: python oauth authorization spotify spotipy

我正在为 Spotify API 使用 Spotipy 包装器。目前我正在排查的功能是用户认证(认证成功后会创建指定名称的播放列表)

我正在使用 Authorization Code Flow

以下是我的代码中与问题相关的部分:

import spotipy
from clientcredentials import user_id, client_id, client_secret
from spotipy.oauth2 import SpotifyOAuth

USER_ID = user_id
SPOTIPY_CLIENT_ID = client_id
SPOTIPY_CLIENT_SECRET = client_secret
SPOTIPY_REDIRECT_URI = 'https://i.kym-cdn.com/entries/icons/original/000/027/475/Screen_Shot_2018-10-25_at_11.02.15_AM.png'
SCOPE = 'playlist-read-private,playlist-modify-private'


def auth_user(self):
    auth_manager = SpotifyOAuth(
        client_id=SPOTIPY_CLIENT_ID, 
        client_secret=SPOTIPY_CLIENT_SECRET, 
        redirect_uri=SPOTIPY_REDIRECT_URI, 
        scope=SCOPE
        )
    sp_authed = spotipy.Spotify(auth_manager=auth_manager)
    return sp_authed

def create_playlist(self, authed):
    new_playlist = authed.user_playlist_create(
        USER_ID, 
        f"{self.artist_name} Favouritism", 
        public=False, 
        collaborative=False, 
        description=self.test_playlist_desc
        )
    return new_playlist


if __name__ == '__main__':
    artist_name = input("Enter artist's name: ")
    print(f"Creating playlist for {artist_name}...")
    af = ArtistFavouritism(artist_name)
    sp_authed = af.auth_user()
    af.create_playlist(sp_authed)

我第一次运行它时,它运行得很好!但是访问令牌过期后,需要刷新令牌。我试图找到一种方法来做到这一点,但在这个过程中可能会搞砸一些事情,所以我用相同的代码创建了一个新应用。

(此外,我确实撤销了对我的应用程序的访问权限,因此我还在 Spotify 开发人员仪表板中创建了一个新应用程序,传输了必要的客户端参数并将重定向 URL 列入白名单)

当我尝试在这个新应用程序中再次运行代码时,它不起作用,并且出现此错误(已检查,这是一个 400 错误):

in refresh_access_token
    raise SpotifyOauthError(
spotipy.oauth2.SpotifyOauthError: error: invalid_client, error_description: Invalid client

请帮忙!我在任何地方都找不到答案,这让我很伤心,哈哈。任何建议将不胜感激! :)

0 个答案:

没有答案