401 - Fixing server time differences?

NWatchAlerts
@NWatchAlerts NeighborWatchAlerts

Hi All!

I'm using OAuth.php along with Abraham's twitteroauth.php version 0.2.0.

The objective is to get my app authorized, store the credentials and do offline posting on the users behalf. I had all this working just fine and then we moved to a new set of servers and upgraded to PHP 5.3.1.

Yesterday I was able to hit the authorization page on twitter and give my app permission but it returned a 401 (see below). This morning we reset the server timezone to CST, changed the timezone in our php.ini to America/Chicago and rebooted and now we don't even get the Twitter Authorization page, we just get an immediate 401.

I suspect this is all about some setting that Twitter needs for our server so I'm hoping someone will tell me the correct mod to make in OAuth.php->generate_timestamp();

The entries we've managed to capture in the error_log are:

  1. [26-Mar-2012 14:43:12 UTC] callback.php: http_code - 401
  2. [26-Mar-2012 14:43:12 UTC] callback.php: http_info - Array
  3. (
  4.     [url] => https://api.twitter.com/oauth/access_token?oauth_consumer_key=cxxxxxxxxxxxxxxxxxxxxxxx&oauth_nonce=xxxxxxxxxxxxxxxxxxxxxxx&oauth_signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1332772989&oauth_verifier=xxxxxxxxxxxxxxxxxxxxxxxxxx&oauth_version=1.0
  5.     [content_type] => text/html; charset=utf-8
  6.     [http_code] => 401
  7.     [header_size] => 1005
  8.     [request_size] => 381
  9.     [filetime] => -1
  10.     [ssl_verify_result] => 0
  11.     [redirect_count] => 0
  12.     [total_time] => 3.292677
  13.     [namelookup_time] => 0.004509
  14.     [connect_time] => 3.067823
  15.     [pretransfer_time] => 3.208732
  16.     [size_upload] => 0
  17.     [size_download] => 1
  18.     [speed_download] => 0
  19.     [speed_upload] => 0
  20.     [download_content_length] => 1
  21.     [upload_content_length] => 0
  22.     [starttransfer_time] => 3.29225
  23.     [redirect_time] => 0
  24.     [certinfo] => Array
  25.         (
  26.         )
  27.  
  28.     [redirect_url] => 
  29. )
  30.  
  31. [26-Mar-2012 14:43:12 UTC] callback.php: http_status - 
  32. [26-Mar-2012 14:43:12 UTC] callback.php: last_api_call - 
  33. [26-Mar-2012 14:43:12 UTC] callback.php: request - 

Thanks

2 years 3 weeks ago

Replies

episod
@episod Taylor Singletary

It's preferable to just keep your server time in UTC so it's agnostic to these kind of issues.

If you want to keep your clock set to CST, you'll probably need to monkey patch the generate_timestamp method to first convert the local time to a UTC representation (using DST-aware time math) and then to the number of seconds since the epoch. There's another approach where you adjust timestamps after the fact based on the date declared in error responses, but it's best to take care of the issue before you send out an initial request.

2 years 3 weeks ago
NWatchAlerts
@NWatchAlerts NeighborWatchAlerts

Yeah, that didnt work. We changed our server timezone to UTC, changed php.ini to Other/UTC, rebooted and nada... we're getting nowhere with this.

Sad part for us is that it took 3 days to get the facebook code up and running and we've been screwing around with this for 3 weeks. It's getting frustrating.

What's the next thing to try?

2 years 3 weeks ago
episod
@episod Taylor Singletary

Are you just changing settings and then retrying auth or are you taking a disciplined approach to this? Are you looking at the response from the API -- does it indicate that it's a timestamp issue or perhaps something else?

Once you've changed your PHP and server settings, what are you doing to better understand the time values that you're actually generating?

It is currently 1:57:57pm pacific daylight savings time on 03/26/2012. That's 20:57:57 UTC and "1332795477" as epoch time in seconds.

2 years 3 weeks ago
NWatchAlerts
@NWatchAlerts NeighborWatchAlerts

Frankly, we don't know where to look for the problem. Like I said in my opening post, it was working, we moved to new servers with a slightly different configuration and php 5.3.x vs 5.2.x and in the move it stopped working.

At this point we've checked and double checked everything we can think of and we're not even getting to the Twitter Authorize screen... we request "approval" and before the user ever sees that screen on Twitter our callback gets called with a 401 error.

2 years 3 weeks ago
episod
@episod Taylor Singletary

Make sure that your consumer keys and secrets are the same in both environments. We send back a HTTP header called "Date" in every response, both successful and erroneous, that will tell you exactly what time we consider it "to be" here. You want to make sure your clock is within about 5 minutes of that.

If you're able to negotiate a request token and then move to the oauth/authorize step, it's likely that your consumer keys and secrets are correct. If you're a number of minutes behind on the clock, you might get oauth/request_token to work, but hasten the expiration of oauth/authorize.

Troubleshooting OAuth 1.0A goes over several common things that can go wrong in OAuth. I really really recommend switching to header-based auth instead of the query-string based approach you're taking now -- it greatly separates concerns and lowers the chance of something minor going wrong.

2 years 3 weeks ago
NWatchAlerts
@NWatchAlerts NeighborWatchAlerts

Will follow these individual steps first thing tomorrow...

2 years 3 weeks ago
gkbhardwaj88
@gkbhardwaj88 gautam kumar

how to check twitter server time zone. Because i'm getting 401 unauthorized acces error while using twitter streaming api to consume public stream.

13 weeks 3 days ago