Python 如何使用urllib将参数添加到POST请求的正文中?

Python 如何使用urllib将参数添加到POST请求的正文中?,python,python-3.x,urllib,Python,Python 3.x,Urllib,我需要使用urllib发送此格式的POST请求: POST /oauth/access_token HTTP/1.1 User-Agent: themattharris' HTTP Client Host: api.twitter.com Accept: */* Authorization: OAuth oauth_consumer_key="cChZNFj6T5R0TigYB9yd1w", oauth_nonce="a9900fe68e2573b27a

我需要使用urllib发送此格式的POST请求:

POST /oauth/access_token HTTP/1.1
User-Agent: themattharris' HTTP Client
Host: api.twitter.com
Accept: */*
Authorization: OAuth oauth_consumer_key="cChZNFj6T5R0TigYB9yd1w",
                     oauth_nonce="a9900fe68e2573b27a37f10fbad6a755",
                     oauth_signature="39cipBtIOHEEnybAR4sATQTpl2I%3D",
                     oauth_signature_method="HMAC-SHA1",
                     oauth_timestamp="1318467427",
                     oauth_token="NPcudxy0yU5T3tBzho7iCotZ3cnetKwcTIRlX0iwRl0",
                     oauth_version="1.0"
Content-Length: 57
Content-Type: application/x-www-form-urlencoded

oauth_verifier=uw7NjWHT6OJ1MpJOXsHfNxoAhPKpgI8BlYDhxEjIBY
而对于标题,我是这样添加的:

AccessRequest = urllib.request.Request('https://api.twitter.com/oauth/access_token')
AccessRequest.add_header('Authorization', oauth_header)
添加应留在主体中的参数的正确方法是什么

现在我正在做:

values = {
    'oauth_verifier': verifier
}
data = urllib.parse.urlencode(values).encode('ascii')
resp = urllib.request.urlopen(AccessRequest, data=data)
但一定是出了什么问题,因为服务器返回了HTTP错误401:需要授权。我做错了什么

from urllib import request, parse
data = parse.urlencode({"data":"Hello Word?"}).encode()
req =  request.Request("http://www.naver.com", data=data) # this will make the method "POST"
resp = request.urlopen(req)
若运行此代码,则数据包如下所示

POST / HTTP/1.1
Accept-Encoding: identity
Host: www.naver.com
Content-Type: application/x-www-form-urlencoded
Connection: close
Content-Length: 18
User-Agent: Python-urllib/3.4

data=Hello+Word%3F
如果我将data=parse.urlencode{data:Hello Word?}.encode更改为data=parse.urlencode{oauth_验证器:uw7njwht6oj1mpjoxshfnxfoahpgipgi8blydhxejiby}.encode

这个包看起来像这样

POST / HTTP/1.1
Accept-Encoding: identity
Host: www.naver.com
Content-Type: application/x-www-form-urlencoded
Connection: close
Content-Length: 57
User-Agent: Python-urllib/3.4

oauth_verifier=uw7NjWHT6OJ1MpJOXsHfNxoAhPKpgI8BlYDhxEjIBY
这不是你想要的吗

若运行此代码,则数据包如下所示

POST / HTTP/1.1
Accept-Encoding: identity
Host: www.naver.com
Content-Type: application/x-www-form-urlencoded
Connection: close
Content-Length: 18
User-Agent: Python-urllib/3.4

data=Hello+Word%3F
如果我将data=parse.urlencode{data:Hello Word?}.encode更改为data=parse.urlencode{oauth_验证器:uw7njwht6oj1mpjoxshfnxfoahpgipgi8blydhxejiby}.encode

这个包看起来像这样

POST / HTTP/1.1
Accept-Encoding: identity
Host: www.naver.com
Content-Type: application/x-www-form-urlencoded
Connection: close
Content-Length: 57
User-Agent: Python-urllib/3.4

oauth_verifier=uw7NjWHT6OJ1MpJOXsHfNxoAhPKpgI8BlYDhxEjIBY

这不是你想要的吗?

使用软件包怎么样?问题可能与此问题类似,因为我是为了在较低级别上学习HTTP的考试而做的,所以我宁愿手动操作而不是使用它。@Damotorie已经尝试将其放入构造函数中,但没有解决。也许不是我做错了?检查我的,如果可以安全地说我正在以正确的方式添加数据,我将删除这个。使用这个包怎么样?问题可能与此问题类似,因为我是为了在较低级别上学习HTTP的考试而做的,所以我宁愿手动操作而不是使用它。@Damotorie已经尝试将其放入构造函数中,但没有解决。也许不是我做错了?看看我的,如果可以安全地说我正在以正确的方式添加数据,我会删除这个。是的,但Twitter服务器似乎仍然无法识别授权,所以我一定是做错了什么,这与我在问题评论中链接的另一个问题更相关。是的,但是Twitter服务器似乎仍然无法识别授权,所以我肯定是做错了什么,这与我在评论中提到的另一个问题更相关。