Skip to content

Instantly share code, notes, and snippets.

@richardsondx
Last active April 4, 2017 07:49
Show Gist options
  • Save richardsondx/e3c850ad137bfafb20d5 to your computer and use it in GitHub Desktop.
Save richardsondx/e3c850ad137bfafb20d5 to your computer and use it in GitHub Desktop.
Monkey patch to fix Authentication failure! invalid_credentials: OAuth2::Error jruby 8 , rails 3.2.13 devise 3.2.4 | Force faraday to 'uncompressed' gzip
# In Rails, add this to your Initializer/ folder
#
# Fix by @richardsondx - Richardson Dackam
#
# Thi monkey patch is not actually forcing farady to uncompress the response but it's telling it not to compress
# So it forces Faraday to avoid compressing the response.
# The gresponse from google had the header 'accept-encoding': 'gzip'
# unlike http.net, http.request doesn't automatically uncompress gzip request
# more info about http.request at https://github.com/ruby/ruby/blob/ruby_2_2/lib/net/http.rb
module Faraday
class Request::UrlEncoded < Faraday::Middleware
def call(env)
match_content_type(env) do |data|
params = Faraday::Utils::ParamsHash[data]
env.body = params.to_query(env.params_encoder)
if env.url.to_s == "https://accounts.google.com/o/oauth2/token"
# Will remove any encoding that is happended for that url
env.request_headers['accept-encoding'] = ''
# The following call might be cleanner as it make sure that the response is not gzip*
# env.request_headers["accept-encoding"] = "gzip;q=0,deflate,sdch"
end
end
@app.call env
end
end
end
@veeresh-yh
Copy link

Hi Richardson,

i'm using rails 2.3.15 and added above chunk of code but getting undefined method params_encoder for #Hash.
any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment