简体   繁体   中英

Retrieving OAuth tokens (on server) from Faraday OAuth module (from client)

I'm using Faraday to communicate between two web apps that I am developing ( background and a related question ). I'm using OAuth Tokens as part of the upload/connection form the client app to the server app.

However, I can't seem to find the OAuth tokens in the header or action_dispatch hashes, etc.

Any suggestions?

I found the answer here: https://github.com/technoweenie/faraday/issues#issue/12

Hi there, I ran into the same problem and found that it was because I used the Faraday.default_adapter before my middleware (in a Faraday::Connection contructor block).

Maybe this can help as I see the issue is still open.

Going from:

@connection ||= Faraday::Connection.new(:url => "http://...") do |builder|
  builder.adapter Faraday.default_adapter
  builder.use CookieAuth
end
to

@connection ||= Faraday::Connection.new(:url => "http://...") do |builder|
  builder.use CookieAuth
  builder.adapter Faraday.default_adapter
end
solved the problem for me.

What happens is that when any http method of Connection is called (falling back to Faraday::Connection#run_request), the default_adapter actually performs the request (like in Faraday::Adapter::NetHttp#call:45) before the middleware and so the modified headers are not sent to the server.

Yet the test case passes because the middeware is called (after the request) and updates the request_headers in the env.

To sums things up, just be sure to configure any "request modifier" middleware before any adapter that actually performs the request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM