【问题标题】:The view function did not return a valid response. The return type must be a string, dict视图函数未返回有效响应。返回类型必须是字符串,dict
【发布时间】:2021-03-15 06:26:36
【问题描述】:

使用 Spotify API 和 Flask 我正在尝试扩展 refresh_token 有效性。结果,当我向服务器发送请求时,我收到此错误:

*The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a Response.*

我的代码:

@app.route("/index")
def index():
    if time.time() > session['expires_in']:
        payload = session['refresh_token']
        ref_payload = {
            'grant_type': 'refresh_token',
            'refresh_token':session["refresh_token"]
        }
        header={'Authorization': 'Basic ' + '<CLIENT_ID>:<CLIENT_SECRET'}
        r = requests.post(AUTH_URL, data=ref_payload, headers=header)
    return r

@app.route("/q")
def api_callback():
    session.clear()
    code = request.args.get('code')

    res = requests.post(AUTH_URL, data={
        "grant_type":"authorization_code",
        "code":code,
        "redirect_uri":REDIRECT_URI,
        "client_id":CLIENT_ID,
        "client_secret":CLIENT_SECRET
        })
    res_body = res.json()
    session["token"] = res_body.get("access_token")#token
    session["expires_in"] = res_body.get("expires_in")#time
    session["refresh_token"] = res_body.get("refresh_token")#reflesh token
    return redirect("index")

https://accounts.spotify.com/api/token 被接受为 AUTH_URL

这个问题很可能很常见,但我现在想不出解决方案。提前致谢

【问题讨论】:

    标签: oauth-2.0 token refresh spotify


    【解决方案1】:

    我解决了这个问题。在我的配置文件中,我创建了一个可验证的文件,其中我将我的 client_id 和 client_secret 编码为 base64 格式:

    ID_SEC = CLIENT_ID +':'+ CLIENT_SECRET
    base64_encode = base64.b64encode(ID_SEC.encode()).decode()
    

    在标题之后我编辑授权:

    header={
        'Content-Type':'application/x-www-form-urlencoded',
        'Accept': 'application/json',
        'Authorization': 'Basic {}'.format(base64_encode)
    }
    

    并发送帖子请求:

    r = requests.post(AUTH_URL, data=ref_payload, headers=header)
    

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 2021-07-29
      • 2020-05-06
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      相关资源
      最近更新 更多