RailsアプリからWordPressのREST APIにアクセスしようとしています。全データの取得を考えているのでOAuthを使う必要があるのですが、どこ探してもサンプルが見つかりません。
oauth-xx/oauth-rubyのGemを使って以下のようなコードを書いてみましたが、'No OAuth parameters supplied'というメッセージがデバッグログに表示されます。
ruby
1 @consumer = OAuth::Consumer.new( 2 'Client Key', 3 'Client Secret', 4 {:site => 'http://our-wordpresssite.com', 5 :request_token_path => '/oauth1/request', 6 :access_token_path => '/oauth1/authorize', 7 :authorize_path => '/oauth1/access', 8 :debug_output => true}) 9 10 @request_token = @consumer.get_request_token 11 session[:token] = request_token.token 12 session[:token_secret] = request_token.secret 13 redirect_to @request_token.authorize_url(:oauth_callback => @callback_url) 14
デバッグログは以下のとおりです。
bash
1opening connection to dev-style.vegewel.com:80... 2opened 3<- "POST /oauth1/request HTTP/1.1\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nUser-Agent: OAuth gem v0.5.4\r\nContent-Length: 0\r\nAuthorization: OAuth oauth_callback=\"oob\", oauth_consumer_key=\"vS1LY0Gvouif\", oauth_nonce=\"xxxxx\", oauth_signature=\"2%xxxxx%2BjphX1CCY4%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1540327228\", oauth_version=\"1.0\"\r\nConnection: close\r\nHost: dev-style.vegewel.com\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n" 4<- "" 5-> "HTTP/1.1 400 Bad Request\r\n" 6-> "Server: nginx\r\n" 7-> "Date: Tue, 23 Oct 2018 20:40:30 GMT\r\n" 8-> "Content-Type: text/html; charset=UTF-8\r\n" 9-> "Content-Length: 28\r\n" 10-> "Connection: close\r\n" 11-> "X-Powered-By: PHP/5.6.38\r\n" 12-> "X-UA-Compatible: IE=edge\r\n" 13-> "Access-Control-Allow-Headers: Authorization\r\n" 14-> "Set-Cookie: pll_language=en; expires=Wed, 23-Oct-2019 20:40:30 GMT; Max-Age=31536000; path=/\r\n" 15-> "\r\n" 16reading 28 bytes... 17-> "No OAuth parameters supplied" 18read 28 bytes 19Conn close 20Completed 500 Internal Server Error in 2626ms (ActiveRecord: 27.3ms) 21400 Bad Request excluded from capture: Not configured to send/capture in environment 'development' 22 23OAuth::Unauthorized (400 Bad Request): 24 app/controllers/style/posts_controller.rb:17:in `index' 25This is Wordpress WP REST API - OAuth 1.0a Server settings.
ちなみにClient KeyとClient Secretは以下の画面から取得しています。
コケている場所はAccessTokenを取得する箇所ですが、
何が問題でどのように対応すればいいかがわかりません。
どなたかお分かりになる方いましたらよろしくお願いします。
あなたの回答
tips
プレビュー