Python Spotipy:readthedocs中的简单代码出现异常

Python Spotipy:readthedocs中的简单代码出现异常,python,spotipy,Python,Spotipy,当我从以下位置运行此简单代码时: 我得到了一个例外: Traceback (most recent call last): File "/Users/haodong/Documents/Projects/python-workspace/env3/lib/python3.4/site-packages/spotipy/client.py", line 119, in _internal_call r.raise_for_status() File "/Users/haodong/

当我从以下位置运行此简单代码时:

我得到了一个例外:

Traceback (most recent call last):
  File "/Users/haodong/Documents/Projects/python-workspace/env3/lib/python3.4/site-packages/spotipy/client.py", line 119, in _internal_call
    r.raise_for_status()
  File "/Users/haodong/Documents/Projects/python-workspace/env3/lib/python3.4/site-packages/requests/models.py", line 844, 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/search?q=artist%3ARadiohead&offset=0&type=artist&limit=10

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 19, in <module>
    results = spotify.search(q='artist:' + name, type='artist')
  File "/Users/haodong/Documents/Projects/python-workspace/env3/lib/python3.4/site-packages/spotipy/client.py", line 339, in search
    return self._get('search', q=q, limit=limit, offset=offset, type=type, market=market)
  File "/Users/haodong/Documents/Projects/python-workspace/env3/lib/python3.4/site-packages/spotipy/client.py", line 146, in _get
    return self._internal_call('GET', url, payload, kwargs)
  File "/Users/haodong/Documents/Projects/python-workspace/env3/lib/python3.4/site-packages/spotipy/client.py", line 124, in _internal_call
    headers=r.headers)
spotipy.client.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/search?q=artist%3ARadiohead&offset=0&type=artist&limit=10:
 No token provided
回溯(最近一次呼叫最后一次):
文件“/Users/haodong/Documents/Projects/pythoworkspace/env3/lib/python3.4/site packages/spotipy/client.py”,第119行,在内部调用中
r、 为_状态()提出_
文件“/Users/haodong/Documents/Projects/pythonworkspace/env3/lib/python3.4/site packages/requests/models.py”,第844行,处于raise_for_状态
引发HTTPError(http\u error\u msg,response=self)
requests.exceptions.HTTPError:401客户端错误:url未经授权:https://api.spotify.com/v1/search?q=artist%3ARadiohead&offset=0&type=artist&limit=10
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“test.py”,第19行,在
results=spotify.search(q='artist:'+name,type='artist')
文件“/Users/haodong/Documents/Projects/pythoworkspace/env3/lib/python3.4/site packages/spotipy/client.py”,第339行,搜索中
返回self.\u get('search',q=q,limit=limit,offset=offset,type=type,market=market)
文件“/Users/haodong/Documents/Projects/pythoworkspace/env3/lib/python3.4/site packages/spotipy/client.py”,第146行,在
返回self.\u内部调用('GET',url,payload,kwargs)
文件“/Users/haodong/Documents/Projects/pythoworkspace/env3/lib/python3.4/site packages/spotipy/client.py”,第124行,在内部调用中
headers=r.headers)
spotipy.client.SpotifyException:http状态:401,代码:-1-https://api.spotify.com/v1/search?q=artist%3ARadiohead&offset=0&type=artist&limit=10:
没有提供代币

Spotify Web API最近似乎已经更新,需要对各种请求进行授权


使用将解决此问题。

您需要使用来自www.developer.Spotify.com的Spotify应用程序凭据(客户端ID和客户端机密),将其分配给变量并将其用作对象

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

cid ="Your-client-ID" 
secret = "Your-client-secret"

client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret) 
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

#Then run your query
results = sp.artist_albums(birdy_uri, album_type='album'))

#### ETC ######
更多信息请点击此处:

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

cid ="Your-client-ID" 
secret = "Your-client-secret"

client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret) 
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

#Then run your query
results = sp.artist_albums(birdy_uri, album_type='album'))

#### ETC ######