Gauge invariant formulation of molecular electrodynamics and the multipolar Hamiltonian