簡體   English   中英

request.post和這種筆直的卷毛有什么區別?

[英]what's the difference between requests.post and this straight curl?

requests庫中使用python 3.6.2。

我正在嘗試使用Battle.net API上的oauth2進行身份驗證,並且在第二個請求(用於令牌檢索的請求)上,出現了400錯誤而沒有有用的解釋(“內部服務器錯誤”)。 Battle.net論壇上的某人建議嘗試使用curl進行相同的POST ,並且它可以工作。 (這是我的論壇帖子: https : //us.battle.net/forums/en/bnet/topic/20762236765

我多次檢查了兩者之間的愚蠢差異(可能是拼寫錯誤),但沒有(我也通過復制粘貼從curl中回寫了請求,獲得了相同的錯誤)。 因此,我認為兩者之間在行為上應該有一些實際差異。

這是python代碼:

data = {
         'code': code,
         'redirect_uri': 'https%3A%2F%2Flocalhost%2Foauth2callback',
         'grant_type': 'authorization_code',
         'client_id': CLIENT_ID,
         'client_secret': CLIENT_SECRET,
         'scope': 'sc2.profile'
    }
resp = requests.post('https://eu.battle.net/oauth/token', data=data)

這是卷發:

curl -X POST -s "https://eu.battle.net/oauth/token" \
    -d client_id='<same as above>' \
    -d client_secret='<same as above>'  \
    -d grant_type='authorization_code' \
    -d redirect_uri='https%3A%2F%2Flocalhost%2Foauth2callback' \
    -d scope='sc2.profile'  \
    -d code='<same as above>'

正如我所說,我想應該有所不同,但是我不是http的專家。 標頭中有東西嗎? 如何設置具有相同行為的請求?

您無需手動編碼redirect_uri requests將為您做到這一點。

data = {
     'code': code,
     'redirect_uri': 'https://localhost/oauth2callback',
     'grant_type': 'authorization_code',
     'client_id': CLIENT_ID,
     'client_secret': CLIENT_SECRET,
     'scope': 'sc2.profile'
}
resp = requests.post('https://eu.battle.net/oauth/token', data=data)

等效的curl調用將使用--data-urlencode而不是-d / --data

curl -X POST -s "https://eu.battle.net/oauth/token" \
    -d client_id='<same as above>' \
    -d client_secret='<same as above>'  \
    -d grant_type='authorization_code' \
    --data-urlencode redirect_uri='https://localhost/Foauth2callback' \
    -d scope='sc2.profile'  \
    -d code='<same as above>'

要調試此類問題,您可以發布到https://httpbin.org/post ; 響應將准確顯示POST請求中發送了哪些數據。

暫無
暫無

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

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