Permalink
Browse files

Created our own version of oauth2 Faraday

middleware to conditionally include the client_id
or access_token when making requests to the API.
Thanks @elliottkember for catching this.
  • Loading branch information...
1 parent ed5415b commit df148ed9906d6cc4d3aeba2e22e123abbf613995 @shayne shayne committed Feb 16, 2011
Showing with 32 additions and 4 deletions.
  1. +28 −0 lib/faraday/oauth2.rb
  2. +1 −1 lib/instagram/connection.rb
  3. +3 −3 lib/instagram/oauth.rb
View
@@ -0,0 +1,28 @@
+require 'faraday'
+
+# @private
+module Faraday
+ # @private
+ class Request::OAuth2 < Faraday::Middleware
+ def call(env)
+ params = env[:url].query_values || {}
+
+ if @access_token
+ params.merge!('access_token' => @access_token)
+ env[:request_headers].merge!('Authorization' => "Token token=\"#{@access_token}\"")
+ elsif @client_id
+ params.merge!('client_id' => @client_id)
+ end
+
+ env[:url].query_values = params unless params == {}
+
+ @app.call env
+ end
+
+ def initialize(app, client_id, access_token=nil)
+ @app = app
+ @client_id = client_id
+ @access_token = access_token
+ end
+ end
+end
@@ -15,7 +15,7 @@ def connection(raw=false)
}
Faraday::Connection.new(options) do |connection|
- connection.use Faraday::Request::OAuth2, access_token if access_token
+ connection.use Faraday::Request::OAuth2, client_id, access_token
connection.adapter(adapter)
connection.use Faraday::Response::RaiseHttp5xx
unless raw
View
@@ -7,16 +7,16 @@ def authorize_url(options={})
params = access_token_params.merge(options)
connection.build_url("/oauth/authorize/", params).to_s
end
-
+
# Return an access token from authorization
def get_access_token(code, options={})
options[:grant_type] ||= "authorization_code"
params = access_token_params.merge(options)
post("/oauth/access_token/", params.merge(:code => code), raw=false, unformatted=true)
end
-
+
private
-
+
def access_token_params
{
:client_id => client_id,

0 comments on commit df148ed

Please sign in to comment.