尝试对Spotify进行身份验证时出错:错误的请求错误

时间:2020-04-16 13:31:15

标签: python jupyter-notebook spotipy

我试图在jupyter笔记本上运行以下代码:

import spotipy 
sp = spotipy.Spotify() 
from spotipy.oauth2 import SpotifyClientCredentials 
import spotipy.util as util

# setting up authorization
cid ="my client id" 
secret = "my client secret"
# saving the info you're going to need
username = 'your_account_number'
scope = 'user-library-read' #check the documentation
authorization_url = 'https://accounts.spotify.com/authorize'
token_url = 'https://accounts.spotify.com/api/token'
redirect_uri ='https://localhost.com/callback/'

token = util.prompt_for_user_token(username,scope,client_id='client_id_number',client_secret='client_secret',redirect_uri='https://localhost.com/callback/')
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

哪个会将我重定向到一个网站。但是,该站点仅包含一个文字“ INVALID_CLIENT:无效的客户端”,仅此而已。当我在提示中粘贴此URL时,我不断收到错误请求错误。感谢您提供的任何帮助或指导。谢谢!

1 个答案:

答案 0 :(得分:0)

这是对我有用的代码:

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy.util as util
import os
from Spotify_isplaying import isplay

os.environ["SPOTIPY_CLIENT_ID"] = '' # client id
os.environ["SPOTIPY_CLIENT_SECRET"] = '' # Secret ID
os.environ["SPOTIPY_REDIRECT_URI"] = '' # Redirect URI
username = "" # username
client_credentials_manager = SpotifyClientCredentials(client_id="SPOTIPY_CLIENT_ID",client_secret="SPOTIPY_CLIENT_SECRET")
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
scope = '' # scope needed for your programme 
token = util.prompt_for_user_token(username, scope)
if token:
    sp = spotipy.Spotify(auth=token)
else:
    print("Can't get token for", username)
# where you put your code