遇到没有满足来自 OAuth2Session 对象的请求的问题。
curl GET "https://api.spotify.com/v1/me/top/artists" -H "Authorization: Bearer <access_token returned from OAuth2Session.access_token within failing program below>
工作得很好。
返回该访问令牌的同一会话在以下位置返回 404:
session.get('/me/top/artists')
[预期,Authorization: Bearer <access_token>
未通过]
session.request('GET', '/me/top/artists')
[阅读源似乎暗示默认情况下包含承载授权标头]
更大的代码片段[您会注意到它看起来与某些示例非常相似]:
from flask import Flask, request, redirect, render_template, url_for
from rauth.service import OAuth2Service
from app import app
import os
spotify = OAuth2Service(name='spotify',
authorize_url=AUTH_URL,
access_token_url=TOKEN_URL,
client_id=os.getenv('SPOTIFY_CLIENT_ID'),
client_secret=os.getenv('SPOTIFY_CLIENT_SECRET'),
base_url='https://api.spotify.com/v1')
@app.route('/authorize')
def authorize():
redirect_uri = url_for('authorized', _external=True)
params = {'response_type': 'code', 'redirect_uri': redirect_uri, 'scope': 'user-top-read'}
return redirect(spotify.get_authorize_url(**params))
@app.route('/authorized')
def authorized():
if not 'code' in request.args:
flash('You did not authorize the request')
return redirect(url_for('welcome'))
redirect_uri = url_for('authorized', _external=True)
data = {'grant_type': 'authorization_code', 'code': request.args['code'], 'redirect_uri': redirect_uri}
def byte_decoder(byte_load):
return json.loads(byte_load.decode())
session = spotify.get_auth_session(data=data, decoder=byte_decoder)
# this is where I am trying the requests
return redirect(url_for('web'))
AUTH_URL
并TOKEN_URL
替换链接,因为我没有 10 个声誉来发布 > 2 个链接。