遇到没有履行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')
[读取源似乎暗示默认包含Bearer Authorization标头]
更大的代码片段[您会注意到它看起来与某些示例非常相似]:
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个声望发布&gt; 2个链接。