0

I'm trying to write Python code for Twitter OAuth authentication. I'm getting a "401 Unauthorized" error code when I attempt to request a token.

In the process of trying to diagnose my problem, I'm going thru each step of the authentication process and trying to undercover any errors I'm making. With regard to generating the "Signature Base String", I found an online tool that tries to help validate signature base strings: http://quonos.nl/oauthTester/

When I use this tool, it complains:

Bad URL encoding!

Both key and value in the POST body need to be URL encoded.

Here is an example Signature Base String that my Python code generates:

POST&https%3A%2F%2Fapi.twitter.com%2F1.1%2Foauth%2Frequest_token&oauth_callback%3Doob%26oauth_consumer_key%3DeXL46FKblmfiXHvmC3wcew%26oauth_nonce%3DTAHTO%2FmlyeJ1x9FrgFixosZPYVhvWLXmq%2BdKKTL1rTY%3D%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1391813822%26oauth_version%3D1.0

When I paste this string into the validator, it says:

    Bad URL encoding!

    Both key and value in the POST body need to be URL encoded.

    In this case: "TAHTO/mlyeJ1x9FrgFixosZPYVhvWLXmq+dKKTL1rTY" is bad

I'm very confused because all key/value pairs in the URL are, in fact, URL encoded (I'm assuming "URL encoded" means "percent encoded" here.)

Is there anything wrong with my base string here?

Edit:

The actual HTTP request headers I'm sending to Twitter to request a token are:

POST /1.1/oauth/request_token HTTP/1.1
Accept-Encoding: identity
Content-Length: 0
Connection: close
Accept: */*
User-Agent: Python-urllib/3.2
Host: api.twitter.com
Content-Type: application/x-www-form-urlencoded format
Authorization: OAuth oauth_callback="oob", oauth_consumer_key="eXL46FKblmfiXHvmC3wcew", oauth_nonce="nBcVYSqv8FEi0d7MEs8%2BqtqvdYA9JcbnW%2BVqoP%2FGlrI%3D", oauth_signature="WT9c3U5Puam7dEnMt3DWDsyVAHw%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1391815422", oauth_version="1.0"
Siler
  • 8,976
  • 11
  • 64
  • 124
  • Yes, the terms "URL encoded" and "percent encoded" are synonyms. Care to show us the exact HTTP request(s) you're sending to Twitter? – Matt Ball Feb 07 '14 at 23:17
  • @Matt Ball, yes I edited the post to include the actual headers – Siler Feb 07 '14 at 23:20
  • Check out the code I've done to the Twitter firehouse API. I was having a 401 error and also a possible solution http://stackoverflow.com/questions/9487918/twitter-stream-using-oauth-in-python-behaving-differently-on-two-equally-configu – Eduardo Feb 07 '14 at 23:28
  • @Eduardo, yeah I tried syncing the system clock... still gives me a 401. – Siler Feb 07 '14 at 23:29
  • Would you mind to add the code you use to generate the authorization URL? – Eduardo Feb 07 '14 at 23:30

0 Answers0