Does anyone here have an experience with OAuth2 and Facebook - https://github.com/scrogson/oauth2? I get an “Error validating client secret” when exchanging code for access_token. I used this solution in different app 3 months ago and it worked (but I don’t have an access to code now - so I can miss something really simple here).
When I try to exchange it manually (I send client_id, client_secret, request_uri and code to https://graph.facebook.com/v2.8/oauth/access_token) It works - I get the token (and user information later).
Here is what I have:
The controller:
defmodule TheApp.AuthController do
use TheApp.Web, :controller
require Logger
def callback(conn, %{"grant_type" => provider, "auth_code" => code}) do
# Exchange an auth code for an access token
client = get_token!(provider, code)
Logger.info "See client #{inspect client}"
.....
end
defp get_token!("facebook", code), do: Facebook.get_token!(code: code)
.....
end
Facebook strategy module:
defmodule Facebook do
use OAuth2.Strategy
require Logger
alias OAuth2.Strategy.AuthCode
defp config do
[strategy: __MODULE__,
site: "https://graph.facebook.com",
authorize_url: "https://www.facebook.com/dialog/oauth",
token_url: "/v2.8/oauth/access_token",
client_id: "16......48",
client_secret: "ed.......c2",
redirect_uri: "http://localhost:4200/"]
end
def client do
config()
|> OAuth2.Client.new()
end
def get_token!(params \\ [], headers \\ []) do
Logger.info "Comming params#{inspect params}"
Logger.info "What's in client#{inspect client()}"
OAuth2.Client.get_token!(client(), params)
end
# Strategy Callbacks
def get_token(client, params, headers) do
Logger.info "Get token client#{inspect client}"
Logger.info "Get token params#{inspect params}"
client
|> put_header("Accept", "application/json")
|> AuthCode.get_token(params, headers)
end
end