Once elected, Mr. Obama took an interest in curbing surveillance. “He would ask me about various issues that relate to the topic of the day — how do you come up with policies that make sure that security and liberty are not mutually exclusive?” recalled Senator Ron Wyden, Democrat of Oregon and a leading critic of surveillance policies then and now.

Mr. Obama was a sponsor of a bill in 2005 to raise the standard required for federal agents using administrative subpoenas known as national security letters to obtain business records without court order. He joined other Democrats fighting the renewal of the Patriot Act until it was amended to address civil liberties concerns, then voted for its extension in 2006 after a compromise, breaking with Mr. Wyden who voted no.

Mr. Obama’s 2007 speech at the Woodrow Wilson International Center for Scholars came after the revelation that President George W. Bush had authorized warrantless surveillance in terrorism cases without permission from the Foreign Intelligence Surveillance Court. A presidential candidate, Mr. Obama criticized Mr. Bush’s “false choice between the liberties we cherish and the security we provide.”

But as a former Obama aide put it recently, “The rhetoric was probably sharper than his votes.” By summer 2008, with the Democratic nomination secured and the White House now a real possibility, Mr. Obama voted for legislation essentially ratifying Mr. Bush’s surveillance programs. Mr. Obama realized he would “take my lumps” from the left and said it “was not an easy call for me,” but he argued that putting the programs under the jurisdiction of the intelligence court restored accountability.

As a result, after he won the election, surveillance issues were off his agenda; instead, he focused on banning interrogation techniques he deemed torture and trying, futilely, to close the prison at Guantánamo Bay, Cuba. “There wasn’t really any serious discussion of what N.S.A. was up to,” said a former intelligence official, who like others did not want to be named describing internal conversations.

Mr. Obama was told before his inauguration of a supposed plot by Somali extremists to attack the ceremony, what David Axelrod, his adviser, called a “welcome-to-the-N.B.A. moment before the first game.” Although the report proved unfounded, it reinforced to Mr. Obama the need to detect threats before they materialized. “The whole Somali threat injected their team into the realities of national security in a tangible and complicated way,” recalled Juan C. Zarate, the departing counterterrorism adviser to Mr. Bush who worked with the Obama team on the threat.