play! framework 1.2.5のTwitter-OAuthサンプルでエラー
play! framework 1.2.5のtwitter-oauthサンプルを動かしてみたが、以下のエラーが発生した。
Error connecting to twitter: OAuth.Error: NOT_AUTHORIZED - Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.
エラー内容を基にconsumer keyとconsumer secretを確認したが、どちらもTwitter Applicationで生成したものと同じ値を設定していた。
原因その1: OAuthのURL誤り
サンプルソースのTwitter OAuth URLに古いもの?が設定されていた。
なお、play!2系のサンプルでは修正されていた。
Application.java
変更前
private static final ServiceInfo TWITTER = new ServiceInfo( "http://twitter.com/oauth/request_token", "http://twitter.com/oauth/access_token", "http://twitter.com/oauth/authorize", "consumer key", "consumer secret" );
変更後
private static final ServiceInfo TWITTER = new ServiceInfo( "https://api.twitter.com/oauth/request_token", "https://api.twitter.com/oauth/access_token", "https://api.twitter.com/oauth/authorize", "consumer key", "consumer secret" );
原因その2: Callback URLの設定漏れ
こちらは私の設定漏れ。Twitter ApplicationのCallback URLに、「http://<自IPアドレス>:9000/auth」を設定する必要がある。
以下のページを参考に設定した。
Playframework2とScalaでTwitter APIを操作する - えんたつの記録