Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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授权代码流返回不完整的响应_Python_Django_Oauth_Spotify - Fatal编程技术网

Python Spotify授权代码流返回不完整的响应

Python Spotify授权代码流返回不完整的响应,python,django,oauth,spotify,Python,Django,Oauth,Spotify,我有一个django服务器,我希望执行spotify授权代码流 以下是我创建的基本框架: 用户打开spotify/loginurl SpotifyLoginView将它们重定向到https://accounts.spotify.com/authorizeurl spotify服务器回调到spotify/callback端点 SpotifyCallbackView向https://accounts.spotify.com/api/token获取身份验证令牌 url.py views.py 此

我有一个django服务器,我希望执行spotify授权代码流

以下是我创建的基本框架:

  • 用户打开
    spotify/login
    url
  • SpotifyLoginView
    将它们重定向到
    https://accounts.spotify.com/authorize
    url
  • spotify服务器回调到
    spotify/callback
    端点
  • SpotifyCallbackView
    https://accounts.spotify.com/api/token
    获取身份验证令牌
url.py views.py
此外,如果我尝试使用该访问令牌,我会返回一个
401
HTTP错误

$ curl -H "Authorization: Bearer <my acess token>" https://api.spotify.com/v1/me


{
  "error" : {
    "status" : 401,
    "message" : "Unauthorized."
  }
}   
$curl-H“授权:持票人”https://api.spotify.com/v1/me
{
“错误”:{
“状态”:401,
“消息”:“未经授权。”
}
}   
这是怎么回事?

在向发出POST请求以获取初始访问令牌时,您必须使用“授权码”作为
grant\u type
。在
handle\u callback()方法中:

 response = requests.post(
    "https://accounts.spotify.com/api/token",
    data={
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": request.build_absolute_uri(
            reverse("spotify callback")
        ),
    },
    headers=AUTH_HEADER,
)
在向发出POST请求时,您必须使用“授权码”作为
grant\u type
,以获取初始访问令牌。在
handle\u callback()方法中:

 response = requests.post(
    "https://accounts.spotify.com/api/token",
    data={
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": request.build_absolute_uri(
            reverse("spotify callback")
        ),
    },
    headers=AUTH_HEADER,
)
$ curl -H "Authorization: Bearer <my acess token>" https://api.spotify.com/v1/me


{
  "error" : {
    "status" : 401,
    "message" : "Unauthorized."
  }
}   
 response = requests.post(
    "https://accounts.spotify.com/api/token",
    data={
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": request.build_absolute_uri(
            reverse("spotify callback")
        ),
    },
    headers=AUTH_HEADER,
)