A controversial proposal to restrict how the National Security Agency collects Americans' telephone records failed to advance in the House of Representatives by a narrow margin Wednesday, a victory for the Obama administration, which has spent weeks defending the program.

Lawmakers voted 217 to 205 to defeat the proposal from an unlikely coalition of liberal and conservative members. Those lawmakers had joined forces in response to revelations by Edward Snowden, a former NSA contractor, that the agency has collected the phone records of millions of Americans — a practice that critics say goes beyond the kind of collection that has been authorized by Congress.

The plan, sponsored by Michigan Reps. Justin Amash, a Republican, and John Conyers, a Democrat, would have restricted the collection of the records, known as metadata, only when there was a connection to relevant ongoing investigations. It also would have required that secret opinions from the Foreign Intelligence Surveillance Court be made available to lawmakers and that the court publish summaries of each opinion for public review.