class OAuth2::Client


An OAuth2 client.

For a quick example of how to authenticate an HTTP::Client with OAuth2 if you already have an access token, check the OAuth2 module description.

This class also provides methods to build authorize URIs and get access tokens with different methods, as specified by RFC6749.


require "oauth2"

client_id = "some_client_id"
client_secret = "some_client_secret"
redirect_uri = "http://some.callback"

# Create oauth client, optionally pass custom URIs if needed,
# if the authorize or token URIs are not the standard ones
oauth2_client ="", client_id, client_secret,
  redirect_uri: redirect_uri)

# Build an authorize URI
authorize_uri = oauth2_client.get_authorize_uri

# Redirect the user to `authorize_uri`...
# ...
# When http://some.callback is hit, once the user authorized the access,
# we resume our logic to finally get an access token. The callback URL
# should receive an `authorization_code` parameter that we need to use.
authorization_code = request.params["code"]

# Get the access token
access_token = oauth2_client.get_access_token_using_authorization_code(authorization_code)

# Probably save the access token for reuse... This can be done
# with `to_json` and `from_json`.

# Use the token to authenticate an HTTP::Client
client ="", tls: true)

# And do requests as usual
client.get "/some_path"

# If the token expires, we can refresh it
new_access_token = oauth2_client.get_access_token_using_refresh_token(access_token.refresh_token)

You can also use an OAuth2::Session to automatically refresh expired tokens before each request.

Defined in:


Class Method Summary

Instance Method Summary

Instance methods inherited from class Reference

==(other : self)
, dup dup, hash hash, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, same?(other : Reference)
same?(other : Nil)
, to_s(io : IO) : Nil to_s

Instance methods inherited from class Object

!=(other) !=, !~(other) !~, ==(other) ==, ===(other)
===(other : YAML::Any)
===(other : JSON::Any)
, =~(other) =~, class class, crystal_type_id crystal_type_id, dup dup, hash hash, inspect(io : IO)
, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ")
, to_s
to_s(io : IO)
, to_yaml
to_yaml(io : IO)
, try(&block) try

Class methods inherited from class Object

==(other : Class) ==, ===(other) ===, cast(other) : self cast, clone clone, dup dup, from_json(string_or_io) : self
from_json(string_or_io, root : String) : self
, from_yaml(string : String) : self from_yaml, hash hash, inspect(io) inspect, name : String name, nilable? nilable?, to_s(io) to_s, |(other : U.class) forall U |

Class Method Detail

def : String, client_id : String, client_secret : String, port = 443, scheme = "https", authorize_uri = "/oauth2/authorize", token_uri = "/oauth2/token", redirect_uri : String | Nil = nil) #

[View source]

Instance Method Detail

def get_access_token_using_authorization_code(authorization_code) : AccessToken #

Gets an access token using an authorization code, as specified by RFC6749, Section 4.1.1

[View source]
def get_access_token_using_client_credentials(scope = nil) : AccessToken #

Gets an access token using client credentials, as specified by RFC 6749, Section 4.4.2

[View source]
def get_access_token_using_refresh_token(refresh_token, scope = nil) : AccessToken #

Gets an access token using a refresh token, as specified by RFC6749, Section 6

[View source]
def get_authorize_uri(scope = nil, state = nil) : String #

Builds an authorize URI, as specified by RFC6749, Section 4.1.1

[View source]