In fact, it is an exercise on Hartshorne, Ex 2.4 to the second chapter of it (p.79):
Let $A$ be a ring and $(X,\mathcal{O}_X)$ be a scheme. Given a morphism $f:X\longrightarrow \operatorname{Spec} A$, we have an associated map on sheaves $f^\sharp :\mathcal{O}_{\operatorname{Spec} A}\longrightarrow f_*\mathcal{O}_X$. Taking global sections we obtain a homomorphism $A\longrightarrow \Gamma (X,\mathcal{O}_X)$. Thus there is a natural map $\alpha:\operatorname{Hom}_{\mathcal{Sch}}(X,\operatorname{Spec} A)\longrightarrow \operatorname{Hom}_{\mathcal{Rings}}(A,\Gamma (X,\mathcal{O}_X))$. How to show that $\alpha$ is bijective?
My approach is to construct an inverse for $\alpha$. Starting from a ring hom $\phi :A\longrightarrow \Gamma (X,\mathcal{O}_X)$, and an affine covering $X=\bigcup_i \operatorname{Spec} B_i$,restricting $\Gamma (X,\mathcal{O}_X)$ to $\mathcal{O}_X (\operatorname{Spec}B_i)$, we get maps $\phi_i:A\longrightarrow B_i$, thus inducing $(f_i,f_{i}^{\sharp}):\operatorname{Spec}B_i\longrightarrow \operatorname{Spec} A$. Then I want to glue these $\operatorname{Spec}B_i$ together to get a morphism $(f,f^\sharp):(X,\mathcal{O}_X)\longrightarrow (\operatorname{Spec}A,\mathcal{O}_{\operatorname{Spec}A})$. I get stuck here: I don't know how to glue them together and verify that $(f,f^\sharp)$ is independent of the covering $\{ \operatorname{Spec}B_i\}_i$. Is my idea right? Will someone be kind enough to give me some hints on this problem? Thank you very much!
I think it's unfortunate that Hartshorne doesn't say, "you can glue morphisms". Here's a general lemma that might help:
It seems to me that one way to avoid worrying about canonicity is to take the family of all affine open sets in $X$ as your cover. There are some remaining technical points in order to even apply the lemma; hopefully I can comment on those later.