Closed kitplummer closed 5 years ago
You need to change config :lei_api, ExOauth2Provider
to config :ex_oauth_provider, ExOauthProvider
Thanks for the quick response! I did try that. :) But got this on startup:
Compiling 25 files (.ex)
Generated lei_api app
[info] Running LeiApiWeb.Endpoint with cowboy 2.6.3 at 0.0.0.0:4000 (http)
[info] Access LeiApiWeb.Endpoint at http://localhost:4000
You have configured application :ex_oauth_provider in your configuration file,
but the application is not available.
This usually means one of:
1. You have not added the application as a dependency in a mix.exs file.
2. You are configuring an application that does not really exist.
Please ensure :ex_oauth_provider exists or remove the configuration.
And then the same error as before:
[info] GET /api/v1/accounts
[debug] Processing with LeiApiWeb.API.V1.UserController.index/2
Parameters: %{}
Pipelines: [:api, :api_protected]
[info] Sent 500 in 46ms
[error] #PID<0.599.0> running LeiApiWeb.Endpoint (connection #PID<0.598.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /api/v1/accounts
** (exit) an exception was raised:
** (RuntimeError) No `:repo` found in ExOauth2Provider configuration.
Please set up the repo in your configuration:
config :ex_oauth_provider, ExOauthProvider,
repo: MyApp.Repo
How does your router.ex
look? The :otp_app
should be set there, for the app config to be loaded:
defmodule LeiApiWeb.Router do
use Phoenix.Router
use PhoenixOauth2Provider.Router, otp_app: :lei_api
# ...
end
Think I've set it up with the right parts:
defmodule LeiApiWeb.Router do
use LeiApiWeb, :router
use Pow.Phoenix.Router
use PhoenixOauth2Provider.Router, otp_app: :lei_api
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
# BEGIN added for Pow
pipeline :protected do
plug Pow.Plug.RequireAuthenticated,
error_handler: Pow.Phoenix.PlugErrorHandler
end
pipeline :api_protected do
plug ExOauth2Provider.Plug.VerifyHeader, otp_app: :my_app, realm: "Bearer"
plug ExOauth2Provider.Plug.EnsureAuthenticated
end
scope "/api/v1", LeiApiWeb.API.V1 do
pipe_through [:api, :api_protected]
resources "/accounts", UserController
end
scope "/" do
pipe_through :browser
pow_routes()
end
scope "/" do
pipe_through :api
oauth_api_routes()
end
scope "/" do
pipe_through [:browser, :protected]
oauth_routes()
end
scope "/", LeiApiWeb do
pipe_through [:browser, :protected]
resources "/queries", QueryController
end
scope "/", LeiApiWeb do
pipe_through :browser
get "/", PageController, :index
end
....
end
You have to change:
plug ExOauth2Provider.Plug.VerifyHeader, otp_app: :my_app, realm: "Bearer"
To
plug ExOauth2Provider.Plug.VerifyHeader, otp_app: :lei_api, realm: "Bearer"
I've improved the error message. There is no ExOauthProvider
module, it was a typo, and the error message will now output the :otp_app
setting, so you would instead see this:
No `:repo` found in ExOauth2Provider configuration.
Please set up the repo in your configuration:
config :my_app, ExOauth2Provider,
repo: MyApp.Repo
Thanks for letting me know 😄
Sorry to bug. Am following the post: https://dreamconception.com/tech/phoenix-full-fledged-api-in-five-minutes/ - or trying to. New to all things Phoenix and Elixir.
I get all the way through the authorization step and can get a token. But when I run
curl http://localhost:4000/api/v1/accounts -H "Authorization: Bearer 0d840c6ae3aea3fcdcd2dbd2b9d884f4811e4a8bdb16168f33334624dfbb04c2"
I get this:It is quite possible I've done something completely stupid. I am a bit confused on the diff between ExOauthProvider, ExOauth2Provider and PhoenixOauth2Provider.
My config.exs does contain the repo config:
Any pointers? Thanks in advance.