A modification of Dror's comment.
This probabilistic algorithm worked for me.
The main idea is to pick some $z$, compute $m=n - z^2$, factor $m$ with trial division and express it as a sum of two squares if possible. The probability of finding prime $m=4a+1$ or $2p$ is high enough for practical purposes.
- if can't trial factor m goto 2
- if m=x^2+y^2 (the factorization is known) then x^2+y^2+z^2=n. Done
- goto 2
Added Experimental results: 100 random integers of size 1000 bits were solved at average time 3.5 seconds per solution. 10 random integers of size 10000 bits were solved at average time 5.8 minutes.
Here is a pari/gp program and example:
%136 = 3
*** last result computed in 2,210 ms.
%138 = 1200
pl=10^6;\\ bound for trial division, may need change
print("\nfound ",z," "," m=",m,factor(m));