OTTAWA -- The politician Canada and its allies recognize as Venezuela's real leader stood in a Caracas plaza Friday and exhorted his supporters to "stay the course" if he winds up behind bars.

Juan Guaido's defiant pronouncement against President Nicolas Maduro -- whom Canada has branded a dictator who stole an election -- marked the latest dramatic development in Venezuela's political crisis. It followed Guaido's decision two days earlier to declare himself his country's interim leader, two weeks after Maduro's contested inauguration.

But emboldening Venezuela's opposition has been a labour of months, The Canadian Press has learned. Canadian diplomats in Caracas, with their Latin American counterparts, worked to get the country's opposition parties to coalesce behind the one person who emerged strong enough to stand against Maduro: 35-year-old Guaido.

The turning point came Jan. 4 when the Lima Group -- the bloc that includes Canada and more than a dozen Latin American countries -- rejected the legitimacy of Maduro's May 2018 election victory and his looming Jan. 10 inauguration, while recognizing the "legitimately elected" National Assembly, sources say.

"They were really looking for international support of some kind, to be able to hold onto a reason as to why they should unite, and push out somebody like Juan Guaido," said one source.

The Canadian Press interviewed senior Canadian government officials, who spoke on condition of anonymity because of the sensitivity of the crisis in Venezuela. They detailed Canada's role in aiding democratic forces to rescue the once oil-rich country from the economic and political spiral that has forced three million Venezuelans from their homes.

Canada anticipated this week's developments because its diplomats have been keeping in close contact with Guaido and other opposition figures in Venezuela. "We listen to them. We listen to the diaspora in Canada and elsewhere in the world, and we do what we can," said one source.

The quiet Canadian diplomacy was conducted in tandem with Lima Group allies such as Chile, Peru, Colombia and Brazil. It was part of a Canadian diplomatic tradition that included efforts in the 1980s to shield Chilean dissidents fighting the Pinochet dictatorship.

And in 2000, foreign-affairs minister Lloyd Axworthy led a multilateral mission to Peru that ousted strongman Alberto Fujimori, said Canada's former ambassador to Venezuela Ben Rowswell.

"The tradition here is that Canada believes in the principles of human rights and democracy and takes pragmatic measures on the ground to unblock political situations," said Rowswell.

Rowswell said he drew on that tradition while he was in Venezuela, hosting a high-profile award party at the Canadian Embassy to honour a local civil-society leader. The annual gathering sent a message that the world was watching pro-democracy efforts in the face of Maduro's growing authoritarianism.

After Rowswell's 2017 departure, the Lima Group was born and Canada began working within that coalition -- which does not include the United States -- to further human rights and democracy in the hemisphere.

Maduro's May 20, 2018 election victory galvanized the Lima Group's efforts. The group denounced the vote as illegitimate and downgraded diplomatic relations.

The diplomats who remained focused on building bridges with a fractured opposition that was as much at odds with itself as it was with Maduro.

In a November report, the International Crisis Group documented the divisions and urged the groups to set aside their "personal and political rivalries."

The top contenders to lead the opposition were long-time leaders Leopold Lopez and Julio Borges, but there were problems with both. Lopez has been under house arrest since 2014, while Borges is living in exile.

Borges put forth Guaido as a contender, said one source.

Guaido made a clandestine trip to Washington in mid-December to brief U.S. officials on his strategy for dealing with Maduro's Jan. 10 inauguration. He secretly crossed his country's border with Colombia so Venezuelan immigration officials wouldn't know he'd left and prevent his return.

As talks among Venezuelan opposition factions progressed, one source said, they began to set aside their differences. A key realization set in: "This is not about us. This is about the country."

The source said the opposition groups deserve full credit for getting to that point. But it helped that Canadian diplomats "could facilitate conversations with people that were out of the country and inside the country" with other foreign diplomats.

On Jan. 5, Guaido assumed the presidency of the National Assembly, which the Lima Group regards as "the only remaining democratically elected institution in the country."

Four days later, Foreign Affairs Minister Chrystia Freeland telephoned Guaido to "congratulate him on becoming president of the National Assembly and his work on uniting the opposition," said another source.

The next day, Maduro was sworn in as president with support of countries such as Cuba, Russia and China; Freeland said "the Maduro regime is now fully entrenched as a dictatorship."

On Wednesday, after Guaido declared himself to be the interim president, Venezuelans took to the streets in protests across the country. "It's an important day for Venezuela," Freeland said in Davos, Switzerland.

On Friday, Maduro told a news conference he'd be willing to talk to the opposition to settle the question of who leads the country, but he defended his presidency. He also called Guaido's declaration "a desperate act" backed by the U.S.

Canadian officials said that while U.S. leaders such as President Donald Trump, Vice-President Mike Pence and Secretary of State Mike Pompeo have also denounced Maduro, there has been no direct co-ordination between the Lima Group and Washington.

The Prime Minister's Office said Trudeau spoke Saturday with Colombian President Ivan Duque Marquez about the situation. The pair reaffirmed their support of Guaido and committed to continuing "to promote democracy, the rule of law, and human rights in Venezuela" in part through their work with the Lima Group, according to the PMO.

As for this week's rallies, the Venezuelans have full ownership of those.

"It was completely done by the opposition and their people on the ground in Venezuela," said one official. "We couldn't have helped them get to this point ΓÇª if they weren't willing and really putting their necks out."

-- with files from the Associated Press