Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facebook user info is not decoded from JSON #151

Closed
JohnDoneth opened this issue Apr 29, 2024 · 4 comments · Fixed by #168
Closed

Facebook user info is not decoded from JSON #151

JohnDoneth opened this issue Apr 29, 2024 · 4 comments · Fixed by #168

Comments

@JohnDoneth
Copy link

It looks like the result for /me for Facebook is returning a JSON payload but the payload is never decoded and passed to normalize where it fails. I don't believe this a configuration issue but I could be wrong. I've included my config and some of the dbg trail below:

config #=> [
  strategy: Assent.Strategy.Facebook,
  base_url: "https://graph.facebook.com/v4.0",
  authorize_url: "https://www.facebook.com/v4.0/dialog/oauth",
  token_url: "/oauth/access_token",
  user_url: "/me",
  authorization_params: [scope: "email"],
  user_url_request_fields: "email,name,first_name,last_name,middle_name,link",
  auth_method: :client_secret_post,
  session_params: %{state: "b12f74c55b98953535c75f3143cd8d4e475f8c3b2a"},
  redirect_uri: "https://local.host:5301/auth/facebook/callback",
  client_id: "<redacted>",
  client_secret: "<redacted>",
  redirect_path: "/auth/facebook/callback"
]
|> process_user_response() #=> {:ok,
 "{\"email\":\"doneth7\\u0040me.com\",\"name\":\"John Doneth\",\"first_name\":\"John\",\"last_name\":\"Doneth\",\"id\":\"7425858337461382\"}"}
** (FunctionClauseError) no function clause matching in Access.get/3
   (elixir 1.16.1) lib/access.ex:307: Access.get("{\"email\":\"doneth7\\u0040me.com\",\"name\":\"John Doneth\",\"first_name\":\"John\",\"last_name\":\"Doneth\",\"id\":\"7425858337461382\"}", "id", nil)
   (assent 0.2.9) lib/assent/strategies/facebook.ex:92: Assent.Strategy.Facebook.normalize/2
@rubynho
Copy link

rubynho commented Oct 28, 2024

Hey @JohnDoneth! Did you discover anything to solve the issue? Having the same problem.

@JohnDoneth
Copy link
Author

JohnDoneth commented Oct 29, 2024

Hey @JohnDoneth! Did you discover anything to solve the issue? Having the same problem.

@rubynho Yes, I forked the Assent.Strategy.Facebook module and decoded the payload with Jason.

@impl true
  def fetch_user(config, access_token) do
    with {:ok, fields} <- Config.fetch(config, :user_url_request_fields),
         {:ok, client_secret} <- Config.fetch(config, :client_secret) do
      params = [
        appsecret_proof: appsecret_proof(access_token, client_secret),
        fields: fields,
        access_token: access_token["access_token"]
      ]

      # This decoding from JSON is the only difference between this module and
      # the original from Assent.
      case OAuth2.fetch_user(config, access_token, params) do
        {:ok, json} -> {:ok, Jason.decode!(json)}
        {:error, error} -> {:error, error}
      end
    end
  end

@danschultzer
Copy link
Collaborator

It's probably the content type header that has changed in the API response. Assent only looks for an application/json content type header, but graph APIs sometimes sends different content types. Adding an accept header may fix it.

@danschultzer
Copy link
Collaborator

It was the content type, adding the accept header makes it work. Fixed with #168.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants