为什么在执行POST请求时出现未授权的错误(401)?

问题描述 投票:0回答:1

我想知道为什么在执行对该URL的请求时出现非授权错误。我已经抓取了令牌,但是我不知道为什么我仍然会收到错误消息。任何帮助是极大的赞赏。到目前为止,这是我的代码:

import requests
from bs4 import BeautifulSoup
from urllib import parse

url = "https://www.skatedeluxe.com/de/vans-chima-pro-2-schuh-black-alpine_p134487"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like 
Gecko) Chrome/80.0.3987.87 Safari/537.36'}

s = requests.Session()

#data = {"product_id":"111382","option_id":"20","quantity":1,"children":[]}

response = s.post(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")

token = soup.find("html")['data-token']

addToCartURL = "https://www.skatedeluxe.com/api2/cart/items?token=" + token
print(addToCartURL)
response2 = s.post(addToCartURL, headers=headers)
print(response2)
python request python-requests http-status-code-401 get-request
1个回答
0
投票

您没有像这样将令牌传递到标题中:

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like 
Gecko) Chrome/80.0.3987.87 Safari/537.36', 
'Authorization': <token_here>
}
© www.soinside.com 2019 - 2024. All rights reserved.