More descriptive error message on OAuth request_token failure (#31)
* More descriptive error message on request failure * Request JSON responses from API, Decode JSON responses in HTTP function, Adjust error handling * Clean up and format affected functions
This commit is contained in:
parent
c5927b4dd7
commit
f0f53d5a7a
@ -101,17 +101,17 @@ defmodule Ueberauth.Strategy.Twitter do
|
|||||||
|
|
||||||
defp fetch_user(conn, token) do
|
defp fetch_user(conn, token) do
|
||||||
params = [{"include_entities", false}, {"skip_status", true}, {"include_email", true}]
|
params = [{"include_entities", false}, {"skip_status", true}, {"include_email", true}]
|
||||||
|
|
||||||
case Twitter.OAuth.get("/1.1/account/verify_credentials.json", params, token) do
|
case Twitter.OAuth.get("/1.1/account/verify_credentials.json", params, token) do
|
||||||
{:ok, %{status_code: 401, body: _, headers: _}} ->
|
{:ok, %{status_code: 401, body: _, headers: _}} ->
|
||||||
set_errors!(conn, [error("token", "unauthorized")])
|
set_errors!(conn, [error("token", "unauthorized")])
|
||||||
{:ok, %{status_code: status_code, body: body, headers: _}} when status_code in 200..399 ->
|
|
||||||
body = Ueberauth.json_library().decode!(body)
|
|
||||||
|
|
||||||
|
{:ok, %{status_code: status_code, body: body, headers: _}} when status_code in 200..399 ->
|
||||||
conn
|
conn
|
||||||
|> put_private(:twitter_token, token)
|
|> put_private(:twitter_token, token)
|
||||||
|> put_private(:twitter_user, body)
|
|> put_private(:twitter_user, body)
|
||||||
|
|
||||||
{:ok, %{status_code: _, body: body, headers: _}} ->
|
{:ok, %{status_code: _, body: body, headers: _}} ->
|
||||||
body = Ueberauth.json_library().decode!(body)
|
|
||||||
error = List.first(body["errors"])
|
error = List.first(body["errors"])
|
||||||
set_errors!(conn, [error("token", error["message"])])
|
set_errors!(conn, [error("token", error["message"])])
|
||||||
end
|
end
|
||||||
|
@ -15,9 +15,30 @@ defmodule Ueberauth.Strategy.Twitter.OAuth.Internal do
|
|||||||
|> OAuther.sign(url, extraparams, creds)
|
|> OAuther.sign(url, extraparams, creds)
|
||||||
|> OAuther.header
|
|> OAuther.header
|
||||||
|
|
||||||
HTTPoison.get(url, [header], params: params)
|
HTTPoison.get(url, [header, {"Accept", "application/json"}], params: params)
|
||||||
|
|> decode_body()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def decode_body({:ok, response}) do
|
||||||
|
content_type =
|
||||||
|
Enum.find_value(response.headers, fn
|
||||||
|
{"content-type", val} -> val
|
||||||
|
_ -> nil
|
||||||
|
end)
|
||||||
|
|
||||||
|
case content_type do
|
||||||
|
"application/json" <> _ ->
|
||||||
|
json_body = Ueberauth.json_library().decode!(response.body)
|
||||||
|
json_response = %{response | body: json_body}
|
||||||
|
{:ok, json_response}
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
{:ok, response}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def decode_body(other), do: other
|
||||||
|
|
||||||
def params_decode(resp) do
|
def params_decode(resp) do
|
||||||
resp
|
resp
|
||||||
|> String.split("&", trim: true)
|
|> String.split("&", trim: true)
|
||||||
|
@ -17,6 +17,18 @@ defmodule Ueberauth.Strategy.Twitter.OAuth do
|
|||||||
request_token: "/oauth/request_token",
|
request_token: "/oauth/request_token",
|
||||||
site: "https://api.twitter.com"]
|
site: "https://api.twitter.com"]
|
||||||
|
|
||||||
|
defmodule ApiError do
|
||||||
|
@moduledoc "Raised on OAuth API errors."
|
||||||
|
|
||||||
|
defexception [:message, :code]
|
||||||
|
|
||||||
|
def message(e = %{code: nil}), do: e.message
|
||||||
|
|
||||||
|
def message(e) do
|
||||||
|
"#{e.message} (Code #{e.code})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def access_token({token, token_secret}, verifier, opts \\ []) do
|
def access_token({token, token_secret}, verifier, opts \\ []) do
|
||||||
opts
|
opts
|
||||||
|> client()
|
|> client()
|
||||||
@ -73,19 +85,22 @@ defmodule Ueberauth.Strategy.Twitter.OAuth do
|
|||||||
|
|
||||||
defp consumer(client), do: {client.consumer_key, client.consumer_secret, :hmac_sha1}
|
defp consumer(client), do: {client.consumer_key, client.consumer_secret, :hmac_sha1}
|
||||||
|
|
||||||
defp decode_response({:ok, %{status_code: 200, body: body, headers: _}}) do
|
defp decode_response({:ok, %{status_code: 200, body: body}}) do
|
||||||
params = Internal.params_decode(body)
|
params = Internal.params_decode(body)
|
||||||
token = Internal.token(params)
|
token = Internal.token(params)
|
||||||
token_secret = Internal.token_secret(params)
|
token_secret = Internal.token_secret(params)
|
||||||
|
|
||||||
{:ok, {token, token_secret}}
|
{:ok, {token, token_secret}}
|
||||||
end
|
end
|
||||||
defp decode_response({:ok, %{status_code: status_code, body: _, headers: _}}) do
|
|
||||||
{:error, "#{status_code}"}
|
defp decode_response({:ok, %{status_code: status_code, body: %{"errors" => [error | _]}}}) do
|
||||||
|
{:error, %ApiError{message: error["message"], code: error["code"]}}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp decode_response({:error, %{reason: reason}}) do
|
defp decode_response({:error, %{reason: reason}}) do
|
||||||
{:error, "#{reason}"}
|
{:error, "#{reason}"}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp decode_response(error) do
|
defp decode_response(error) do
|
||||||
{:error, error}
|
{:error, error}
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user