-
-
Notifications
You must be signed in to change notification settings - Fork 621
Advanced usage
Advanced configurations for specific cases.
When connect to slow oauth providers or slow network, we may encounter Net::ReadTimeout
exception. In this case, pass additional connection options to OAuth2::Client
to extend timeout as below.
client = OAuth2::Client.new(client_id, client_secret, site: '', connection_opts: { request: { timeout: 300 } })
connection_opts[:request][:timeout]
open/read timeout in seconds. Ref Faraday custom usage.
Default Faraday adapter use Net::HTTP
which has timeout 60 seconds.
By default, responses from OAuth providers will be parsed to hash, depend on their Content-Type
. Supported content type:
# Parse as JSON:
application/json
text/javascript
application/hal+json
application/vnd.collection+json
application/vnd.api+json
# i.e.
'{"access_token": "aaaaaaaa", "additional_data": "additional"}'
=> {"access_token"=>"aaaaaaaa", "additional_data"=>"additional"}
# Parse as XML:
text/xml
application/rss+xml
application/rdf+xml
application/atom+xml
application/xml
# i.e.
'<access_token>aaaaaaaa</access_token>'
=> {"access_token"=>"aaaaaaaa"}
# Parse as query string
application/x-www-form-urlencoded
# i.e.
"access_token=aaaaaaaa&additional_data=additional"
=> {"access_token"=>"aaaaaaaa", "additional_data"=>"additional"}
# Return whole body as parsed content
text/plain
If failed to parse, parser will return whole response body, which may cause exception on layers above if they try to get access token from parsed response.
To enforce parser, pass :parse
option to #get_token
. By this, oauth2
will ignore Content-Type
and use configured parser.
client.auth_code.get_token(auth_code, redirect_uri: redirect_uri, parse: :query)
:parse
accepts built-in parsers similar to use Content-type
above:
-
:xml
- parse response body as XML. -
:json
- parse response body as JSON. -
:query
- parse response body as query string. -
:text
- parse response body astext/plain
.
Besides, :parse
also accepts a Proc/callable object to manually parse response body. This parser proc should return a hash contains access_token.
e.g. If whole response body is access token, the code below will build access_token object.
parser = Proc.new do |body, _response| {access_token: body} end client.auth_code.get_token(auth_code, redirect_uri: redirect_uri, parse: parser)
Moreover, if you frequently use a custom proc parser or have parser for new Content-type
, you should register your new parser to avoid keep define proc. In rails, initializer is a good place to register.
e.g.
OAuth2::Response.register_parser(:mytext, ['text/plain', 'application/custom-text-content-type']) do |body| {access_token: body} end # Then client.auth_code.get_token(auth_code, redirect_uri: redirect_uri, parse: :mytext) # Or just remove it if OAuth provider return `Content-Type: application/custom-text-content-type`. client.auth_code.get_token(auth_code, redirect_uri: redirect_uri)
To get response body or Content-Type
to determine correct parser, set ENV['OAUTH_DEBUG'] == 'true'
before performing request. Once enabled, faraday
will log response info.
I, [...] INFO -- request: GET https://api.example.com/success
I, [...] INFO -- request: User-Agent: "Faraday v1.0.1"
I, [...] INFO -- response: Status 200
I, [...] INFO -- response: Content-Type: "text/awesome"
I, [...] INFO -- response: yay