WASHINGTON (AP) — The U.S. House of Representatives narrowly approved continuing the National Security Agency’s secret collection of hundreds of millions of Americans’ phone records after a fierce debate pitting privacy rights against the government’s efforts to thwart terrorism.

Wednesday night’s 217-205 vote was unlikely to be the final word on government intrusion to defend the U.S. and Americans’ civil liberties.

A vote marked the first chance for lawmakers to take a stand on the secret surveillance program since former NSA systems analyst Edward Snowden leaked classified documents last month that spelled out the monumental scope of the government’s activities.

Republican Rep. Justin Amash had challenged the program as an indiscriminate collection of phone records, saying his effort was to defend the U.S. Constitution and “defend the privacy of every American.” On Twitter, he vowed: “We fight on.”

His measure, offered as an addition to a $598.3 billion defense spending bill for 2014, would have canceled the statutory authority for the NSA program, ending the agency’s ability to collect phone records and metadata under the USA Patriot Act unless it identified an individual under investigation.

The House later voted to pass the overall defense bill, 315-109.

The issue created unusual political coalitions in Washington, with the Obama administration, national security leaders in Congress and the Republican establishment facing off against libertarian-leaning conservatives and some liberal Democrats.

The measure challenging the surveillance program faces strong opposition in the Senate and from the White House and is unlikely to survive in a final spending bill.

“Have 12 years gone by and our memories faded so badly that we forgot what happened on Sept. 11?” Rep. Mike Rogers, the Republican chairman of the Intelligence committee, said in pleading with his colleagues to back the program.

With a flurry of letters, statements and tweets, both sides lobbied furiously in the hours prior to the vote in the Republican-controlled House. The director of national intelligence, James Clapper, warned against dismantling a critical intelligence tool.

Since the Sept. 11, 2001, attacks, Congress has authorized — and a Republican and a Democratic president have signed — an extension of the powers to search records and conduct roving wiretaps in pursuit of terrorists.

Since the disclosures this year, however, lawmakers have said they were shocked by the scope of the two programs — one to collect records of hundreds of millions of calls and the other allowing the NSA to sweep up Internet usage data from around the world that goes through nine major U.S.-based providers.

“We’ve really gone overboard on the security side,” said Rep. Peter Welch, who said it was time for a full debate on behalf of U.S. taxpayers about programs long cloaked in secrecy and part of an annual classified intelligence budget of about $30 billion.

The White House and the director of the NSA, Army Gen. Keith Alexander, made last-minute appeals to lawmakers, urging them to oppose the amendment. Eight former attorneys general, CIA directors and national security experts wrote in a letter to lawmakers that the two programs are fully authorized by law and “conducted in a manner that appropriately respects the privacy and civil liberties interests of Americans.”

White House press secretary Jay Carney issued an unusual statement on the eve of Wednesday’s vote, arguing that the change would “hastily dismantle one of our intelligence community’s counterterrorism tools.”

Proponents of the NSA programs argue that the surveillance operations have been successful in thwarting at least 50 terror plots across 20 countries, including 10 to 12 directed at the United States. Among them was a 2009 plot to strike at the New York Stock Exchange.

The overall defense spending bill would provide the Pentagon with $512.5 billion for weapons, personnel, aircraft and ships plus $85.8 billion for the war in Afghanistan for the next budget year.

The total, which is $5.1 billion below current spending, has drawn a veto threat from the White House, which argues that it would force the administration to cut education, health research and other domestic programs in order to boost spending for the Pentagon.

Also Wednesday, the House backed an amendment that would require the president to seek congressional approval before sending U.S. military forces into the 2-year-old civil war in Syria.

The administration is moving ahead with sending weapons to vetted rebels, but Obama and members of Congress have rejected the notion of U.S. ground forces.

The House also adopted an amendment barring funds for military or paramilitary operations in Egypt amid concerns about the measure jeopardizing the United States’ longstanding relationship with the Egyptian military.

The overall bill must be reconciled with whatever measure the Democratic-controlled Senate produces.

___

Associated Press writer Donna Cassata contributed.

Copyright 2013 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.