If you want to prove the statement in question using a gluing argument, then you need to prove the result in the special case that $X=\mathrm{Spec}(B)$ is also affine. This will allow you to conclude that your maps on intersections of affine opens agree on overlaps (because $\mathrm{Hom}(\mathrm{Spec}(B),\mathrm{Spec}(A))\rightarrow\mathrm{Hom}(A,B)$ is a bijection).
The result for $X=\mathrm{Spec}(B)$ is proved in Hartshorne if I remember correctly. In any case, the idea is that a ring map $\varphi:A\rightarrow B$ induces a continuous map $\alpha:\mathrm{Spec}(B)\rightarrow\mathrm{Spec}(A)$ under which the inverse image of a standard open $D(f)\subseteq\mathrm{Spec}(A)$ is $D(\varphi(f))$ ($f\in A$). The homomorphism $\varphi$ naturally induces, for each $f\in A$, a ring map $A_f\rightarrow B_{\varphi(f)}$, i.e., a map $\mathcal{O}_{\mathrm{Spec}(A)}(D(f))\rightarrow\mathcal{O}_{\mathrm{Spec}(B)}(D(\varphi(f))$, compatible with restriction, and since the sets $D(f)$ give a base for the topology of $\mathrm{Spec}(A)$, this is extends uniquely to a sheaf map $\mathcal{O}_{\mathrm{Spec}(A)}\rightarrow\alpha_*\mathcal{O}_{\mathrm{Spec}(B)}$. For $\mathfrak{q}\in\mathrm{Spec}(B)$, $\mathfrak{p}=\alpha(\mathfrak{q})=\varphi^{-1}(\mathfrak{q})$, the stalk map $A_\mathfrak{p}\rightarrow B_\mathfrak{q}$ is also induced (via the universal property of localization) by $\varphi$, and is easily seen to be local (by construction). This morphism $\alpha$ also recovers $\varphi$ on global sections.
Uniqueness follows ultimately because of the requirement that the stalk maps of a morphism of locally ringed spaces are local and because morphisms of sheaves are determined by the morphisms on stalks. The requirement that the stalk maps be local forces $\alpha(\mathfrak{q})=\varphi^{-1}(\mathfrak{q})$, and then, again using the universal property of localization, there is a unique map of stalks $A_{\varphi^{-1}(\mathfrak{q})}\rightarrow B_\mathfrak{q}$ compatible with $\varphi$. In summary, if you want to recover $\varphi$ on global sections and you want your stalk maps to be local, you only have one choice (everything is induced by the universal property of localization).
I want to point out that this argument actually extends to prove the result you're after with $X$ replaced not just by an arbitrary scheme, but by an arbitrary locally ringed space. This means the result actually has nothing to do with gluing (since an arbitrary locally ringed space needn't be built from affine schemes). The crucial thing is (as mentioned above) that for a morphism $\alpha:X\rightarrow\mathrm{Spec}(A)$ the stalk map $\alpha_x^\sharp:A_{f(x)}\rightarrow\mathcal{O}_{X,x}$ is local for any $x\in X$. Note that here $f(x)$ is a prime ideal of $A$ and $A_{f(x)}$ is the localization at that prime (the stalk of the structure sheaf of $\mathrm{Spec}(A)$ at $f(x)$). If $\alpha^\sharp:A\rightarrow\mathcal{O}_X(X)$ is the map on global sections of the morphism $\alpha$, then its compatibility with the stalk map $\alpha_x^\sharp$ and the fact that $\alpha_x^\sharp$ is local actually implies that $f(x)$ is the inverse image of the maximal ideal $\mathfrak{m}_x\subseteq\mathcal{O}_{X,x}$ under the ring map $A\rightarrow\mathcal{O}_X\rightarrow\mathcal{O}_{X,x}$ (the first arrow is $\alpha^\sharp$ and the second is taking the stalk at $x$). So the map on global sections determines the morphism $\alpha$ on the underlying topological spaces. Once you know this, it also follows that the stalk maps are uniquely determined.
The point of all this is that the map you want to prove is a bijection is injective. To prove that it is surjective, you basically run the above argument backwards. Given a ring map $\varphi:A\rightarrow\mathcal{O}_X(X)$, you can define $\alpha:X\rightarrow\mathrm{Spec}(A)$ on topological spaces by taking $f(x)$ to the prime ideal that is the inverse image of $\mathfrak{m}_x\subseteq\mathcal{O}_{X,x}$ under the map mentioned in the previous paragraph. You can prove then that for any $f\in A$, $\alpha^{-1}(D(f))$ is $X_{\varphi(f)}$, defined as the set of all $x\in X$ such that $\varphi(f)_x$ is not in the maximal ideal $\mathfrak{m}_x$ of $\mathcal{O}_{X,x}$. This is an open set of $X$ (the analogue of a standard open in an affine scheme), so $\alpha$ is continuous. The universal property of localization together with the fact that $\varphi(f)\vert_{X_{\varphi(f)}}\in\mathcal{O}_X(X_{\varphi(f)})$ is a unit (this follows from the definition of $X_{\varphi(f)}$) shows that the ring map $A\rightarrow\mathcal{O}_X(X)\rightarrow\mathcal{O}_X(X_{\varphi(f)})$ ($\varphi$ followed by restriction from $X$ to $X_{\varphi(f)})$ induces a unique map $A_f\rightarrow\mathcal{O}_X(X_f)$ compatible with restriction. This data is what you need for a map of sheaves $\mathcal{O}_{\mathrm{Spec}(A)}\rightarrow\alpha_*\mathcal{O}_X$. This $\alpha$ recovers $\varphi$ on global sections and proves surjectivity.
Just to explain, the reason I went through all this was to illustrate that the adjunction between the global sections functor and $\mathrm{Spec}$ actually works on the entire category of locally ringed spaces (not just the full subcategory of schemes) and so really has nothing to do with gluing maps on affine patches. I personally (when learning algebraic geometry) found this fact really helped me and shaped the way I think of affine schemes among locally ringed spaces (or just schemes). For example, any locally ringed space $X$ admits a unique morphism to $\mathrm{Spec}(\mathbb{Z})$, and the method of proof I've described shows that it sends a point $x\in X$ to the prime ideal generated by the characteristic of the residue field $ \kappa(x)$.
Assume that two such morphisms $f,g: T \rightarrow X$ exist. Let $x_1,x_2$ be their images of the closed point $s \in T$ ($f$ and $g$ map the generic point $\eta \in T$ to the same point of $X$, by definition). Let $V,W \subset X$ be standard affine open subsets of $X$ containing $x_1$ and $x_2$ respectively, then $V \cap W$ contains $f(\eta)=g(\eta)$.
Thus $f$ factors as a morphism $T \rightarrow V$, $g$ as a morphism $T \rightarrow W$.
Then $f$ induces compatible morphisms $f^{\sharp} O(V) \rightarrow R$, $h: O(V \cap W) \rightarrow K$, and $g$ induces comptible morphisms $g^{\sharp} O(W) \rightarrow R$, $h: O(V \cap W) \rightarrow K$.
The compatibilities imply that the two morphisms $O(V) \otimes O(W) \rightarrow O(V \cap W) \overset{h}{\rightarrow} K$ and $O(V) \otimes O(W) \overset{f^{\sharp}\otimes g^{\sharp}}{\rightarrow} R \rightarrow K$ are equal. But it’s straightforward to see that $O(V) \otimes O(W) \rightarrow O(V \cap W)$ is onto, which implies that the image of $h$ is contained in $R$.
Therefore, as $V \cap W$ is affine, $f,g$ factor as the morphisms of affines schemes $T \rightarrow V \cap W$. But if $f_1, g_1$ are their structure maps $O(V \cap W) \rightarrow R$, theirs compositions with $R \subset K$ are both exactly $h$, hence $f_1=g_1$ and $f=g$.
This uniqueness property is far more common than the existence+uniqueness property. It is a necessary and sufficient condition for another property of schemes, separatedness.
A scheme $X$ is separated if the diagonal $X \rightarrow X \times_{\mathbb{Z}} X$ is a closed immersion (one can see it’s always an immersion; there’s also a relative notion of separatedness which isn’t entirely relevant here).
Equivalently, it is enough for $X$ to have an open cover by affine open subsets $U_i$ such that every $U_i \cap U_j \rightarrow U_i \times_{\mathbb{Z}} U_j$ is a closed immersion – ie $U_i \cap U_j$ is affine and $O(U_i) \otimes O(U_j) \rightarrow O(U_i \cap U_j)$ is onto… looks familiar?
More generally, we can consider the following setting: take a dense open subscheme $U$ of a scheme $T$ (eg the generic point in a DVR), and two morphisms $X \rightarrow Y$ that agree on $U$ – must they be equal on all of $X$? If $Y$ is separated and $X$ is reduced, the answer is yes (by a generalization of the above argument).
Best Answer
If we have an extension of our map $\operatorname{Spec}\mathcal{O}_{X,P}\to X'$ as claimed, then it induces the same map on function fields by construction: the map on function fields is given by the induced map at the generic point. But two dominant rational maps inducing the same map on function fields are equivalent - this is covered in section I.4, for instance.
Now let's tackle the extension to a neighborhood of $P$. Let $U=\operatorname{Spec} A\subset X$ be an affine open neighborhood of $P$ and let $U'=\operatorname{Spec} A'\subset X$ be an affine open neighborhood of $P'$, the image of $P$. We have a morphism $A'_{P'}\to A_P$ from the map $\operatorname{Spec} \mathcal{O}_{X,P}\to\operatorname{Spec} \mathcal{O}_{X',P'}$, and since $A'$ is finitely generated over $k$ because $X'$ is of finite type, we can look at the composite map $A'\to A'_{P'}\to A_P$ and deduce a compatible map $A'\to A_f$ where $f$ is the product of all the denominators of the images of the generating set for $A'$. This gives an extension of $\operatorname{Spec} \mathcal{O}_{X,P}\to X$ to $\operatorname{Spec} A_f\to X'$, where $\operatorname{Spec} A_f$ is an open affine neighborhood of $P$ in $X$.