0

我正在尝试为个人项目获取我的个人 Spotify 数据,并且正在使用 Spotipy api 来尝试这样做。主要是按照这篇文章/指南,我写了这段代码:

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy.util as util

cid ="********"
secret = "********"
redirecturi = 'http://localhost:8888'
username = "***"
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret) 
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
scope = 'user-library-read playlist-read-private'
token = util.prompt_for_user_token(username, scope,client_id=cid,client_secret=secret,redirect_uri=redirecturi)
if token:
    sp = spotipy.Spotify(auth=token)
else:
    print("Can't get token for", username)

我收到一条错误消息,说无法读取缓存:.cache-“我的用户名”,python 错误是这样的:

OSError                                   Traceback (most recent call last)
<ipython-input-2-57cc37944df1> in <module>
     16 sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
     17 scope = 'user-library-read playlist-read-private'
---> 18 token = util.prompt_for_user_token(username, scope,client_id=cid,client_secret=secret,redirect_uri=redirecturi)
     19 if token:
     20     sp = spotipy.Spotify(auth=token)

~\anaconda3\lib\site-packages\spotipy\util.py in prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri, cache_path, oauth_manager, show_dialog)
     97 
     98     if not token_info:
---> 99         code = sp_oauth.get_auth_response()
    100         token = sp_oauth.get_access_token(code, as_dict=False)
    101     else:

~\anaconda3\lib\site-packages\spotipy\oauth2.py in get_auth_response(self, open_browser)
    437             # Only start a local http server if a port is specified
    438             if redirect_port:
--> 439                 return self._get_auth_response_local_server(redirect_port)
    440             else:
    441                 logger.warning('Using `%s` as redirect URI without a port. '

~\anaconda3\lib\site-packages\spotipy\oauth2.py in _get_auth_response_local_server(self, redirect_port)
    403 
    404     def _get_auth_response_local_server(self, redirect_port):
--> 405         server = start_local_http_server(redirect_port)
    406         self._open_auth_url()
    407         server.handle_request()

~\anaconda3\lib\site-packages\spotipy\oauth2.py in start_local_http_server(port, handler)
   1225 
   1226 def start_local_http_server(port, handler=RequestHandler):
-> 1227     server = HTTPServer(("127.0.0.1", port), handler)
   1228     server.allow_reuse_address = True
   1229     server.auth_code = None

~\anaconda3\lib\socketserver.py in __init__(self, server_address, RequestHandlerClass, bind_and_activate)
    450         if bind_and_activate:
    451             try:
--> 452                 self.server_bind()
    453                 self.server_activate()
    454             except:

~\anaconda3\lib\http\server.py in server_bind(self)
    136     def server_bind(self):
    137         """Override server_bind to store the server name."""
--> 138         socketserver.TCPServer.server_bind(self)
    139         host, port = self.server_address[:2]
    140         self.server_name = socket.getfqdn(host)

~\anaconda3\lib\socketserver.py in server_bind(self)
    464         if self.allow_reuse_address:
    465             self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
--> 466         self.socket.bind(self.server_address)
    467         self.server_address = self.socket.getsockname()
    468 

OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions

有人可以让我知道我应该如何授予访问权限,或者我做错了什么吗?如果有任何区别,我正在为此使用 Jupyter Notebook。此外,如果您知道获取我所追求的数据的更简单方法,请告诉我。

这是我的第一篇文章,因此对我在这里使用的任何不良做法表示歉意。

4

1 回答 1

0

尝试在另一个环境/IDE 中运行它。第 310 行spotipy/oauth2.py说明Spotipy如果令牌的缓存文件尚未实例化,则会引发此警告。

于 2020-12-13T11:09:07.713 回答