2

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?

Eärendil Baggins
  • 552
  • 1
  • 8
  • 23
  • How about using [this](http://docs.python-requests.org/en/master/) package? – Damotorie Feb 17 '18 at 13:40
  • [this](https://stackoverflow.com/questions/36484184/python-make-a-post-request-using-python-3-urllib) question may looks similar with this questions – Damotorie Feb 17 '18 at 13:42
  • Because I'm doing this for an exam that is about learning HTTP on a lower level, so I'd rather do it manually instead of using that. – Eärendil Baggins Feb 17 '18 at 13:43
  • @Damotorie Already tried putting it in the constructor, doesn't solve. Perhaps it's not there that I'm doing wrong? Check my [other question](https://stackoverflow.com/questions/48835937/cant-get-the-authorization-to-work-in-retrieving-the-access-token-from-twitter) out, I'll delete this one if it's safe to say I'm adding the `data` the right way. – Eärendil Baggins Feb 17 '18 at 13:47

1 Answers1

5
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)

If run this code, packet looks like this.

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

If I change data = parse.urlencode({"data":"Hello Word?"}).encode() to data = parse.urlencode({"oauth_verifier":"uw7NjWHT6OJ1MpJOXsHfNxoAhPKpgI8BlYDhxEjIBY"}).encode()

Packet will look like this

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

Isn't this what you want?

Damotorie
  • 586
  • 7
  • 25
  • Yes it is, but the Twitter server seems to still not recognize the authorization, so I must be doing something else wrong, which is more related to my other question I've linked in the comment to the question. – Eärendil Baggins Feb 17 '18 at 14:36