I need to send a POST request with this format using urllib:
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
While for the header I add it this way:
AccessRequest = urllib.request.Request('https://api.twitter.com/oauth/access_token')
AccessRequest.add_header('Authorization', oauth_header)
What is the correct way to add the parameter which should stay in the body?
Right now I'm doing:
values = {
'oauth_verifier': verifier
}
data = urllib.parse.urlencode(values).encode('ascii')
resp = urllib.request.urlopen(AccessRequest, data=data)
but there must be something wrong since the server returns a HTTP Error 401: Authorization Required. What am I doing wrong?