It is a well known result that the number of integer solutions $(x,y), x>0, y\ge 0$ to $x^2+y^2 = n$ is $\sum_{d|n}\chi(d)$, where $\chi$ is the nontrivial Dirichlet character modulo $4$ such that $\chi(d)=1$ if $d\equiv 1\pmod{4}$, $\chi(d)=-1$ if $d\equiv 3\pmod{4}$, and $\chi(2k)=0$.
My question: Can we extend this to the integers of other imaginary quadratic fields? How many integer solutions are there to $x^2+2y^2 = n$ and $x^2+3y^2 = n$? Can this be generalized?
To derive the result for $x^2+y^2=n$, one extends the notion of a Euler product to the Gaussian integers $\mathbf{Z}[i]$. Suppose $a_n$ is the number of solutions, so $a_n$ is the number of ideals $(\alpha)\in\mathbf{Z}[i]$ such that $N(\alpha)=n$. Using well known characterization of irreducibles in $\mathbf{Z}[i]$, we get $$\sum_{n=1}^{\infty}\frac{a_n}{n^s}=\prod_{(\pi)}\frac{1}{1-N(\pi)^{-s}}=\left(\frac{1}{1-2^{-s}}\right)\prod_{p\equiv 1\pmod{4}}\left(\frac{1}{1-p^{-s}}\right)^2\prod_{q\equiv 3\pmod{4}}\left(\frac{1}{1-q^{-2s}}\right). $$ After algebraic manipulation followed by introducing the zeta function, we can write the RHS as $$ \zeta(s)\left(\prod_{p\equiv 1\pmod{4}}\frac{1}{1-p^{-s}}\right)\left(\prod_{q\equiv 3\pmod{4}}\frac{1}{1+q^{-s}}\right)=\zeta(s)\prod_{p}\frac{1}{1-\chi(p)p^{-s}}=\zeta(s)\sum_{n=1}^{\infty}\frac{\chi(n)}{n^s}$$ due to standard results on multiplicativity. Finally, due to Dirichlet convolution, we can match up coefficients to what we originally started with to get the desired $a_n = \sum_{d|n}\chi(d)$.
In the case of $x^2+3y^2=n$, we would work over $\mathbf{Z}[\omega]$. The irreducibles here are precisely $1-\omega$, $q\equiv 2\pmod{3}$ where $q$ is a rational prime, and $\pi$ such that $p = \pi\bar\pi$ where $p\equiv 1\pmod{3}$ is a rational prime. Similarly in $\mathbf{Z}[\sqrt{-2}]$, the conditions for irreducibility depend on the residue class of the norm modulo $8$. Thus, using a Dirichlet character modulo $3$ or modulo $8$, could we also extend the Euler product to these rings - thus allowing to answer such questions?
Finally, can this be generalized to the integers of $\mathbf{Q}(\sqrt{-d})$ in general so as to give a way to count the number of solutions to $x^2+dy^2=n$ in terms of Dirichlet characters? Would this work differently in quadratic extensions with different class numbers? Moreover, there are $\varphi(m)$ characters modulo $m$, so which one would we use?
I hope the jumble of thoughts and questions above are not completely ignorant, naive, and/or trivial (or completely bogus). Any ideas/thoughts/answers are greatly appreciated.
EDIT: Lemma 3.25 in David Cox's Primes of the form $x^2+ny^2$ actually gives a formula for this sort of computation: Let $m$ be a positive odd number relatively prime to $n>1$. Then, the number of ways that $m$ is properly represented by a reduced form of discriminant $-4n$ is $2\displaystyle\prod_{p|n}\left(1+\left(\frac{-n}{p}\right)\right).$ While Cox states that he doesn't deal with number of representations by forms in his book, the proof of this is dealt with in the exercises.