Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Spotify API以';无效的用户名';尝试获取当前播放的歌曲时_Python_Spotify - Fatal编程技术网

Python Spotify API以';无效的用户名';尝试获取当前播放的歌曲时

Python Spotify API以';无效的用户名';尝试获取当前播放的歌曲时,python,spotify,Python,Spotify,我正在尝试使用Spotify API获取当前播放的歌曲。我看了文件,看来你得先授权。如您所见,我正在浏览器中打开授权页面,一旦成功授权,我将向端点发送POST请求,以便获取访问令牌,稍后使用GET请求将其传递给另一个端点。但是,我得到的回报是无效用户名。我做错了什么 import requests import webbrowser import urllib SPOTIFY_CLIENT_ID = 'spam' SPOTIFY_CLIENT_SECRET = 'eggs' def get_

我正在尝试使用Spotify API获取当前播放的歌曲。我看了文件,看来你得先授权。如您所见,我正在浏览器中打开授权页面,一旦成功授权,我将向端点发送POST请求,以便获取访问令牌,稍后使用GET请求将其传递给另一个端点。但是,我得到的回报是
无效用户名
。我做错了什么

import requests
import webbrowser
import urllib

SPOTIFY_CLIENT_ID = 'spam'
SPOTIFY_CLIENT_SECRET = 'eggs'

def get_currently_played_song():
    webbrowser.open(
        'https://accounts.spotify.com/authorize?' + 
        urllib.parse.urlencode(
            {
                'client_id': SPOTIFY_CLIENT_ID,
                'response_type': 'code',
                'scope': 'user-read-currently-playing',
                'redirect_uri': 'https://www.spotify.com/us/'
            }
        )
    )

    response = requests.post(
        'https://accounts.spotify.com/api/token',
        data={
            'grant_type': 'client_credentials',
        },
        auth=(SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET)
    )
    access_token = response.json()['access_token']
    current_song = requests.get(
        'https://api.spotify.com/v1/me/player/currently-playing',
        headers={
            'Authorization': f'Bearer {access_token}',
        }
    )
    print(current_song.json())

if __name__ == '__main__':
    get_currently_played_song()
…打印:

{'error': {'status': 404, 'message': 'Invalid username'}}