WASHINGTON (AP) — A lame-duck president, an empowered opposition, a looming election: They’re hardly the ingredients for a resounding White House triumph.

Yet President Barack Obama clinched a huge victory on the Iran nuclear deal in Congress this past week when Senate Democrats blocked GOP attempts to get a disapproval resolution to his desk and frustrated House Republicans settled for passing two related measures destined to go nowhere.

The outcome was especially notable for a White House with a history of bungling legislative initiatives on Capitol Hill, and a president known for a hands-off relationship with lawmakers, even his own Democrats.

This time was different, according to administration officials and lawmakers of both parties. The reasons involved policy, politics and a president looking for one last big success to burnish his foreign policy legacy

Obama threw everything into uniting Democrats behind the accord, writing letters and flying lawmakers on Air Force One with him to Africa. House Minority Leader Nancy Pelosi, D-Calif., gave the White House the names of 57 House Democrats to call in August; Obama called each one.

“The White House gets great credit for this. This is the crowning jewel achievement in foreign policy of the White House,” said Democratic Rep. Alan Lowenthal of California, a member of the House Foreign Affairs Committee who was lobbied heavily by both sides before finally deciding in favor of the deal.

“I’m glad it’s over,” he added. “This has sucked the air out of Congress.”

The president was aided by a complex congressional review process that gave minority Democrats unusual leverage, and he was backed by the same Capitol Hill leaders who helped him push his health care law through a bitterly divided Congress in 2010.

Some GOP congressional aides are calling the Iran deal the president’s “Foreign Policy Obamacare” because it was secured over unanimous Republican opposition and will be Democrats’ to defend for years. Frustrated in defeat, opponents complain they barely stood a chance.

___

Years in the making, the accord was aimed at curbing Iran’s nuclear program in exchange for billions of dollars in relief from international sanctions. From the start, the agreement faced fierce GOP opposition.

As negotiations progressed this year, partisan hues sharpened even though foreign affairs are traditionally more divorced from partisan politics and the U.S. was negotiating along with five international partners: Britain, Germany, France, Russia and China.

In March, Senate Republicans led by freshman Sen. Tom Cotton of Arkansas infuriated the White House and Democrats by releasing an open letter to the leaders of Iran warning them about the limits of any deal with Obama.

That same month, Israeli Prime Minister Benjamin Netanyahu spoke to Congress at the invitation of House Speaker John Boehner, R-Ohio, and warned against the deal. The occasion was arranged without the knowledge of the White House. Pelosi fumed visibly throughout the speech.

By hardening partisan lines, both events made it harder for Democrats to buck Obama in the end.

___

In the days after the agreement was finalized in July, the White House set up a command center in the West Wing basement to oversee outreach efforts to Congress. It became known as the “anti-war room” and housed staffers with whiteboard lists of lawmakers for and against.

Obama and senior officials were in frequent contact with Senate Minority Leader Harry Reid, D-Nev., and his No. 2, Sen. Dick Durbin of Illinois, along with Pelosi.

As lawmakers left Washington for their August recess, the White House was cautiously optimistic despite the opposition of New York Sen. Chuck Schumer, a prominent Jewish Democrat.

With lawmakers spread around the country, Energy Secretary Ernie Moniz emerged as one of the administration’s most effective messengers. A floppy-haired nuclear physicist who spent most of his career outside of politics, Moniz was seen as a highly credible voice.

He popped up in surprising places.

As the administration courted Sen. Heidi Heitkamp, D-N.D., Moniz called into a radio show her brother hosted. When Moniz learned that a top Israeli official was in Montana to step up pressure on Democratic Sen. Jon Tester, the secretary contacted a local newspaper for an interview.

Throughout August, the White House’s message to Democrats was simple: If you support the deal, say so publicly.

___

Durbin organized a vote-gathering operation out of his ornate office on the third floor of the Capitol.

He invited ambassadors from the other countries in the accord to meet with 30 Senate Democrats. The diplomats’ message: This is the best deal you will get.

New York Sen. Kirsten Gillibrand, in backing the deal, referenced the ambassadors’ meeting in her statement, and concluded: “Going back to the negotiation table is not an option.”

That sentiment emerged as a refrain from other Democrats.

As September began and lawmakers were getting ready to return to Washington, the decisive 34th Senate Democrat announced support for the deal — enough to ensure Democrats could uphold Obama’s veto of any congressional disapproval resolution.

But by the time that announcement came from Sen. Barbara Mikulski of Maryland, the White House and Democrats were already aiming higher: 41, the number needed to bottle up the disapproval resolution with a filibuster.

___

Last Tuesday, coordinated announcements from Sens. Ron Wyden of Oregon, Richard Blumenthal of Connecticut and Gary Peters of Michigan — all seen as possible Democratic opponents of the deal — put backers over the top.

One question remained: Would Democratic and independent senators hold together and block the disapproval resolution with a filibuster?

Reid was confident, or at least decided it was smart to pretend he was.

As the Senate came back into session, but before the C-SPAN microphones were on, Reid approached Senate Majority Leader Mitch McConnell of Kentucky on the Senate floor and told him Democrats had the votes to block the disapproval resolution. When aides asked Reid later if that was really the case, the Nevadan just shrugged.

Pressure from the powerful pro-Israel lobby AIPAC (the American Israel Public Affairs Committee) was intense to allow a final vote on the disapproval resolution, even if senators were going to oppose it.

Democratic senators were repeatedly losing their nerve and coming to Reid or the White House to explain they couldn’t back a filibuster. They were told, in essence, not to screw things up.

“They faced a lot of pressure to go the other way,” Durbin said.

In the end Republicans fell two votes short of the 60 needed to move the disapproval resolution to a final vote.

The White House and Democrats had won. “An historic step,” Obama exulted in a statement.

Opponents took bitter consolation in the fact that Republicans united against the deal, even if they couldn’t stop it.

“Let’s face it. On foreign policy, the administration holds more cards,” said Republican Sen. Bob Corker of Tennessee, chairman of the Foreign Relations Committee.

___

Associated Press writers Mary Clare Jalonick and Deb Riechmann contributed to this report.

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