Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: how to use OAuth2::AccessToken.post?

OAuth2::AccessToken.post() method is specified like this in the documentation:

(Object) post(path, opts = {}, &block)

I'm trying to pass some arguments, but seems that I*m doing it wrong:

response = token.post('/oauth/create.js', {:title => "title", :description => "desc"})

The parameters are never reaching the method, values are always nil. So, what is the correct way of using the post method with arguments? And what is that &block?

I'm also getting WARNING: Can't verify CSRF token authenticity. This might be contributing to the problem as well. The case is that I'm using OAuth api from the outside of the app. OAuth 2 is implemented via Doorkeeper gem.

Update: The CSRF warning is gone now after I defined scopes. Also I manage to use this post() method with arguments by providing the as part of the url: "?title=test&...". Still would be nice to know how to use this method as documented.

like image 514
Alexander Savin Avatar asked Apr 26 '12 12:04

Alexander Savin


2 Answers

The body in a POST or PUT is accessed via the options body param. No documentation on this. Had to look in the oauth client code itself to discover this:

https://github.com/intridea/oauth2/blob/ebe4be038ec14b3496827d29cb224235e1c9f468/lib/oauth2/client.rb

Your example, with correct body would be:

response = token.post('/oauth/create.js', {body: {:title => "title", :description => "desc"}})
like image 84
Matt Avatar answered Nov 04 '22 15:11

Matt


You can use the block to pass parameters to post request:

token.post('/oauth/create.js') do |request|
  request.params['title'] = "something"
end

OAuth2 gem uses faraday, the request object is a faraday request, so you might want to check other ways to pass parameters along with the request

faraday gem => https://github.com/lostisland/faraday

like image 41
Felipe Elias Philipp Avatar answered Nov 04 '22 15:11

Felipe Elias Philipp