ReqClientCredentials (req_client_credentials v0.1.3)

View Source

Req plugin for OAuth 2.0 client credentials flow authentication. The access token will be cached and reused for subsequent requests, if the response to the :url returns a 401 response then this plugin will refresh the access token (only refreshes one time). If an :audience is included in :client_credentials_params then this plugin will only run if the schema, host, and port of the :url match that of the :audience.

Summary

Functions

attach(req, opts \\ [])

Runs the plugin.

Usage

req =
  Req.new(
    client_credentials_params: [
      audience: "https://api.example.com",
      client_id: System.get_env("EXAMPLE_CLIENT_ID"),
      client_secret: System.get_env("EXAMPLE_CLIENT_SECRET")
    ],
    client_credentials_url: "https://auth.example.com/oauth/token",
  )
  |> ReqClientCredentials.attach()
Req.get!(req, url: "https://api.example.com/path")
#=> %Req.Response{}