The upshot is that you have to know the X display (placed in the DISPLAY environment variable) and the magic cookie (placed in a file, with the filename in the XAUTHORITY environment variable).
The quick-and-dirty way
On the system running X, if you are root or you are the same user who logged in to X, just assume the most common display and cookie files (works on almost any standard desktop install of any distro).
env DISPLAY=:0 XAUTHORITY=/home/whoever/.Xauthority /path/to/my/X/program
The more surefire way
Find them from the environment of an already-running X program. Again, if you are root or the same user who is logged in, this will tell you (if the user is using GNOME):
cat /proc/`pgrep -f ^x-session-manager`/environ \
| ruby -ne 'puts $_.split("\0").select { |e| e =~ /^(DISPLAY|XAUTHORITY)=/ }'