After weeks of haggling, Socialist leader Pedro Sánchez has clinched a vote in parliament to lead Spain for another term as prime minister.
He has secured a four-seat majority in the 350-seat chamber, after sealing an amnesty deal for Catalans involved in a failed bid to secede from Spain.
The conservative Popular Party won elections in July, but leader Alberto Núñez Feijóo failed to form a majority.
Mr Sánchez told MPs that the amnesty deal would help "heal wounds".
His reliance on two Catalan pro-independence parties to form a majority has infuriated opponents, who argue his proposed amnesty deal for hundreds of politicians and activists will trigger another bid for secession and threaten Spain's territorial unity.
Socialist MPs applauded their leader for several minutes when the result of the vote was confirmed but MPs were booed by protesters as they filed out of the Spanish Congress building.
Last weekend tens of thousands of Spaniards took part in protests across Spain, and Mr Feijóo has accused the prime minister of pursuing his own interests rather than his country's.
The Popular Party leader shook his opponent's hand after the vote but later declared to reporters: "I told the president of the government that this was a mistake but he is responsible for what he has just done."
The Socialists were clearly in the hands of those who wanted "recognition of a nation different from that of Spain and a referendum for self-determination", Mr Feijóo declared.
Shortly before the vote, Mr Sánchez linked attempts to question the legitimacy of his new government to part of a global trend. He referred to the presence of former Fox News TV anchor Tucker Carlson at a recent protest outside the Socialist Party headquarters in Madrid.
"We've seen it in the United States, in Brazil and other parts of the world where there is a political right and political far right who do not accept the result of elections," he said.
Several Socialist members of parliament were chased and had eggs thrown at them as they left a cafe near Congress. One egg hit MP Herminio Sánchez and delegates were advised to look after their personal safety in light of the febrile political atmosphere.
Buses hired by right-wing Catholic organisation Hazte Oír drove past the congress building with anti-Sánchez messages on them. One had a picture of Sánchez made to look like Adolf Hitler, with the slogan: "Sánchez dictator."
Pedro Sánchez was given the chance to form a government by King Felipe VI in early October, after the Popular Party leader had tried and failed twice to persuade parliament to back him. In the end Mr Feijóo fell four seats short of a simple majority in parliament, despite the support of the far-right Vox party.
The Socialists first secured the backing of a pro-independence party - the Catalan Republican Left (ERC) - who are in power in Spain's north-eastern region.
Then they clinched a controversial amnesty deal with the more radical Together for Catalonia (Junts), led by Carles Puigdemont, who fled to Brussels to avoid arrest after leading an illegal independence vote in 2017.
The proposed amnesty law would benefit not only the Junts leader, but hundreds of other pro-independence figures too. It would cover actions dating back to 2012 and any arrest warrants not yet served will be scrapped and would likely allow Mr Puigdemont's eventual return from exile.
Much of Spain's judiciary has criticised the proposals and the main judges' association, which is considered conservative, has condemned it as the "beginning of the end of democracy".
Mr Sánchez has rejected claims that his government is reliant on parties that want to break up Spain and he will be sworn in by the king at the Zarzuela Palace in Madrid on Friday.
However. a hint of the fragility of his new government came from Ione Belarra, whose Podemos party is part of a broad left-wing alliance called Sumar, which is part of the new ruling coalition.
Podemos has so far not been invited to run any of the ministries in the new government. "The Socialist Party likes a leftist regime, a docile left that does whatever the Socialist Party wants," she complained.