【问题标题】:Spotipy: simple code from readthedocs got exceptionSpotipy:来自 readthedocs 的简单代码出现异常
【发布时间】:2017-05-30 00:54:40
【问题描述】:

当我从Spotify's Docs 运行这个简单的代码时:

import spotipy

birdy_uri = 'spotify:artist:2WX2uTcsvV5OnS0inACecP'
spotify = spotipy.Spotify()

results = spotify.artist_albums(birdy_uri, album_type='album')
albums = results['items']
while results['next']:
    results = spotify.next(results)
    albums.extend(results['items'])

for album in albums:
    print(album['name'])

我遇到了这个异常:

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

【问题讨论】:

    标签: python spotipy


    【解决方案1】:

    您需要使用来自 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 ######
    

    更多信息在这里:Client Credential Flow

    【讨论】:

      【解决方案2】:

      Spotify Web API 最近好像更新了,各种请求都需要授权。

      使用authorized requests 可以解决这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-23
        • 1970-01-01
        • 2015-11-14
        • 2018-03-13
        • 1970-01-01
        • 2011-05-10
        • 2017-10-12
        相关资源
        最近更新 更多