defmodule SimpleshopWeb.DebugLive do use SimpleshopWeb, :live_view require Logger @impl true def mount(_params, _session, socket) do send(self(), :fetch_shops) {:ok, socket |> assign(:loading, true) |> assign(:shops, []) |> assign(:error, nil) |> assign(:response_details, nil)} end @impl true def handle_info(:fetch_shops, socket) do config = Application.get_env(:simpleshop, :printify) token = Keyword.get(config, :access_token) if token && token != "your_personal_access_token_here" do url = "https://api.printify.com/v1/shops.json" # Try without the Content-Type header - some APIs don't like it for GET requests headers = [ {"Authorization", "Bearer #{token}"}, {"User-Agent", "SimpleshopMVP"} ] Logger.info("Making request to: #{url}") Logger.info("Token (first 20 chars): #{String.slice(token, 0, 20)}...") case Req.get(url, headers: headers) do {:ok, %{status: 200, body: shops}} -> Logger.info("Successfully fetched #{length(shops)} shops") {:noreply, socket |> assign(:loading, false) |> assign(:shops, shops) |> assign(:error, nil) |> assign(:response_details, nil)} {:ok, %{status: status, body: body, headers: resp_headers}} -> Logger.error("Failed to fetch shops: #{status}") Logger.error("Response body: #{inspect(body)}") Logger.error("Response headers: #{inspect(resp_headers)}") error_msg = case body do %{"errors" => errors} -> "API Error: #{inspect(errors)}" %{"message" => message} -> "API Error: #{message}" _ -> inspect(body) end {:noreply, socket |> assign(:loading, false) |> assign(:error, "HTTP #{status}: Authentication failed") |> assign(:response_details, %{ status: status, body: body, error_msg: error_msg })} {:error, error} -> Logger.error("HTTP error fetching shops: #{inspect(error)}") {:noreply, socket |> assign(:loading, false) |> assign(:error, "Network error: #{inspect(error)}") |> assign(:response_details, nil)} end else {:noreply, socket |> assign(:loading, false) |> assign(:error, "Access token not configured. Please add it to config/dev.exs") |> assign(:response_details, nil)} end end @impl true def render(assigns) do ~H"""
Fetching your shop information...
<%= @error %>
<%= if @response_details do %>Debug Information:
Status Code: <%= @response_details.status %>
Error Message: <%= @response_details.error_msg %>
<%= Jason.encode!(@response_details.body, pretty: true) %>
Troubleshooting Tips:
No shops found for this account.
Shop ID (Use this!)
<%= shop["id"] %>
Sales Channel
<%= shop["sales_channel"] || "N/A" %>
Next step: Copy the Shop ID above and paste it into
config/dev.exs
shop_id: "<%= shop["id"] %>",
<%= Jason.encode!(shop, pretty: true) %>