LoginSignup
5
7

More than 5 years have passed since last update.

Twitter Streaming API の401エラーでハマったお話

Posted at

個人で運営してるTwitter Streaming APIを使ったサービスで、突然401エラーが出るようになった。

原因がわからず結構ハマってしまったので、共有のために書いておく。

エラー内容

$ curl --request 'POST' 'https://stream.twitter.com/1.1/statuses/filter.json' --data 'track=twitter' --header 'Authorization: OAuth oauth_consumer_key="XXXXXXXXX", oauth_nonce="XXXXXXXXX", oauth_signature="XXXXXXXXX", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1458266283", oauth_token="XXXXXXXXX", oauth_version="1.0"' --verbose
* About to connect() to stream.twitter.com port 443 (#0)
*   Trying 199.59.148.138... connected
* Connected to stream.twitter.com (199.59.148.138) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_RSA_WITH_AES_128_CBC_SHA
* Server certificate:
*   subject: CN=stream.twitter.com,OU=Twitter Security,O="Twitter, Inc.",L=San Francisco,ST=California,C=US
*   start date:  4月 09 00:00:00 2014 GMT
*   expire date: 12月 30 23:59:59 2016 GMT
*   common name: stream.twitter.com
*   issuer: CN=VeriSign Class 3 Secure Server CA - G3,OU=Terms of use at https://www.verisign.com/rpa (c)10,OU=VeriSign Trust Network,O="VeriSign, Inc.",C=US
> POST /1.1/statuses/filter.json HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: stream.twitter.com
> Accept: */*
> Authorization: OAuth oauth_consumer_key="XXXXXXXXX", oauth_nonce="XXXXXXXXX", oauth_signature="XXXXXXXXX", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1458266283", oauth_token="XXXXXXXXX", oauth_version="1.0"
> Content-Length: 13
> Content-Type: application/x-www-form-urlencoded
> 
< HTTP/1.1 401 Authorization Required
< cache-control: must-revalidate,no-cache,no-store
< connection: close
< content-length: 266
< content-type: text/html
< date: Fri, 18 Mar 2016 03:46:08 GMT
< server: tsa
< www-authenticate: OAuth realm="Firehose"
< x-connection-hash: 0bd058f2a8d6f55b19b3db5285ac134e
< 
<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>\n<title>Error 401 Unauthorized</title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing '/1.1/statuses/filter.json'. Reason:
<pre>    Unauthorized</pre>
</body>
</html>
* Closing connection #0

状況

・アカウントが有効かどうかをチェックする https://api.twitter.com/1.1/account/verify_credentials のAPIは成功するので認証自体が失敗してるわけではない。

・REST APIは成功する。

・しかし、Streaming APIに関してはfilter.jsonもsample.jsonも401エラーで失敗する。

・ローカル環境では、filter.jsonもsample.jsonも成功する。

・サーバーのOpenSSL更新したのでそれが原因かと思って使用してるライブラリのCA用証明書を更新するも意味なし

原因

If you're on Linux, sync your time with an NTP server

サーバーのNTPサーバーが止まってた。。

さくらVPSのハードウェア入れ替えでNTPサーバーが停止する可能性があるってメール着てたのに完全に忘れてた。。

認証に使う時間がおかしなことになってたから401エラーがきてたっぽい

解決

 # /sbin/service ntpd restart
5
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7