簡體   English   中英

Python請求參數不通過

[英]Python requests parameters not going through

我正在嘗試按照來自這些說明https://quizlet.com/api/2.0/docs/authorization-code-flow的 OAuth 流程向 Quizlet 發出發布請求。 我遇到了一個問題,在第 2 步中,我必須使用我從他們的服務器生成的令牌發出一個發布請求,但我沒有成功地將令牌傳遞到 url。 我知道它是正確生成的,但是我在傳遞它並且沒有得到400響應時遇到問題。

更直接地,我的問題是,是否有另一種方法可以包含我試圖通過 url 傳遞的grant_typecode參數,例如通過 post 請求的標頭傳遞它們? 我查看了requests的文檔,但我沒有運氣。

@app.route('/')
@app.route('/index')
def index():
    code = request.args.get('code')
    state = request.args.get('state')
    print("code is " + code)
    r = requests.post("https://api.quizlet.com/oauth/token?grant_type=authorization_code&code=" + code)

    return render_template('index.html')

您必須指定所需的標頭AuthorizationContent-Type

import requests
from requests.auth import _basic_auth_str

client_id = 'YOUR CLIENT ID'
secret = 'YOUR CLIENT SECRET'
code = 'CODE FROM STEP 1'

headers = {
    'Authorization': _basic_auth_str(client_id, secret),
    'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.post('https://api.quizlet.com/oauth/token?grant_type=authorization_code&code={0}'.format(
    code), headers=headers)

print r.status_code
print r.content

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM