ISTANBUL — A little more than two years ago, as the dinner at a NATO meeting in the resort town of Antalya came to a close, Turkish Foreign Minister Mevlüt Cavusoglu took to the stage and asked his guests to join him in a “song for peace.”

The result, captured on video, was a picture of perfect harmony: NATO ministers, as well as the alliance’s Secretary-General Jens Stoltenberg and the European Union’s foreign policy chief Federica Mogherini, locking arms and belting out a rendition of the 1980s charity single “We Are The World.”

But fast-forward two years, and an encore seems unthinkable. NATO’s unity is shaken, with U.S. President Donald Trump taking his time to commit to mutual defense, the alliance’s core principle.

Trump’s erratic approach to NATO may top the alliance’s current list of concerns, but Turkey isn’t far behind.

Publicly, senior NATO officials insist Turkey is as integral to the alliance as ever. Behind the scenes, they have to work hard to ease tensions that have mounted since President Recep Tayyip Erdoğan responded to last summer's coup attempt by purging hundreds of senior military officials, including many NATO officers.

“We see Turkey as an important NATO ally that needs to be supported” — Petr Pavel, chairman of NATO’s Military Committee

Those tensions escalated over the Erdoğan government's efforts to campaign in Europe in favor of a referendum to consolidate the president's political control.

'Needs support'

While Erdoğan ultimately won the referendum, his slide toward authoritarianism and flirtations with Russia have set further alarm bells ringing in Western capitals. And there are signs that these tensions have begun to undermine military cooperation with some NATO allies.

A dispute with Germany this month resulted in Berlin preparing to withdraw troops from Turkey’s Incirlik, the main air base of the U.S.-led coalition against the Islamic State. Earlier this year, Ankara blocked non NATO-member Austria’s cooperation with the alliance, after Austrian ministers repeatedly called for an end to Turkey’s EU accession process.

Concerns also persist over the sweeping purges that followed last July’s failed coup — and to what degree they have weakened Turkey’s military, the second-largest in NATO. While many of the vacancies created by the purge have been filled, the replacements are in many cases less experienced than their predecessors, who were among Turkey's most seasoned officers.

And in Syria, Turkey has threatened to disrupt the coalition’s efforts to retake the city of Raqqa from ISIS.

General Petr Pavel, who as chairman of NATO's Military Committee is the alliance's most senior uniformed official, said relations with Turkey had to be understood in context, and it was only natural after the coup that Ankara would feel “more threatened than other nations."

“Turkey is exposed to both major challenges that NATO is now facing, that is on the one hand, a state actor, Russia, on the other hand, non-state actors, extremism, terrorism and migration,” Pavel said at a POLITICO Playbook event in Brussels. “All these severely affect Turkey directly.”

He added, “We see Turkey as an important NATO ally that needs to be supported."

Complicated partner

Ankara’s relationship with Europe has not recovered from its low point this spring, when a dispute over whether Turkish ministers should be able to campaign in European countries sent tensions flaring.

Germany has some 280 Bundeswehr soldiers, a refueling plane and a number of Tornado jets stationed at this joint American-Turkish air base.

As a result, speeches by the irascible Turkish president skewed ever more anti-European in the run-up to this April’s referendum. Desperate to win over nationalist voters, Erdoğan accused Germany and the Netherlands of “Nazi tactics” and warned that Europeans would not be able to “walk safely in the streets” if they did not change their attitude toward Turkey, to name but two examples.

Erdoğan won the referendum by the narrowest of margins, but those predicting that a newly confident Turkish leader would change tack and improve relations with Europe were sorely disappointed. On the contrary: last month Turkey detained a German translator and a French photographer, prompting complaints from France’s Emmanuel Macron and German's Angela Merkel at the NATO summit in May.

Merkel raised another issue with Erdoğan at the Brussels summit: Incirlik. Germany has some 280 Bundeswehr soldiers, a refueling plane and a number of Tornado jets stationed at this joint American-Turkish air base, from where they fly reconnaissance missions for the anti-ISIS coalition. Earlier this year, Turkey began blocking German MPs from visiting the soldiers. Such routine visits are a vital aspect of Germany’s attitude to its army, which is overseen by parliament rather than the government.

It wasn’t the first time: Turkey blocked German parliamentarians from accessing their soldiers at the base last year, after Germany passed a resolution recognizing the 1915 massacres of Ottoman Armenians as genocide — which Turkey denies — for several months.

This time, Turkey did not relent, enraged that Germany had granted asylum to Turkish soldiers and diplomats accused of supporting last summer’s coup attempt. After talks came to nothing, the German government in early June gave the go-ahead to withdraw its troops and jets stationed at Incirlik; they will likely be redeployed to a base in Jordan.

Technically, the disagreement over Incirlik is not linked to NATO, as the anti-ISIS coalition is led by the U.S. rather than the alliance. (Ankara had initially also blocked visits to the NATO airbase in Konya, but changed its mind in late May.) Yet Turkey’s attempt to use access to the base as political leverage may dent its reputation as a reliable ally, critics say.

“Even though Incirlik is not a NATO issue, it’s affecting Turkey’s image in NATO negatively,” said Marc Pierini, a former EU ambassador to Turkey and visiting scholar at Carnegie Europe. “Turkey has a history of problems accumulating with Germany. Incirlik is another tit-for-tat reaction from the Turkish government, but it resonates much bigger.”

The spillover of Turkey’s domestic troubles into its foreign relations has made Turkey appear as “a less reliable partner,” Pierini added. “That doesn’t make Turkey an enemy — it’s a strategic component of NATO — but it makes Turkey a more complicated partner.”

(No) solidarity

Turkey’s large military and its geopolitical position bridging the Middle East and Europe, bordering both the Mediterranean and the Black seas, make it indispensable for NATO. But because of its location, Turkey needs NATO just as much, given its proximity to Russia to the north and the maelstrom of Middle Eastern conflicts to the south.

Turks refute the accusation that their commitment to the alliance has weakened. They like to tell European critics and journalists to scrutinise their own countries: after all, from their point of view, Germany is refusing to cooperate with its NATO ally Turkey in the fight against terrorism by refusing to extradite suspects.

“How can you summarise NATO in one word? I would say solidarity. What is the main problem in NATO today? I would say lack of solidarity,” said Onur Oymen, who served as Turkey’s representative to NATO between 1997 and 2002. NATO members, he argued, too easily dismissed Ankara’s terrorism concerns.

A longtime member of the opposition party CHP, Oymen nevertheless sides with Erdoğan when it comes to foreign policy. “When anything happens in Europe that hurts us, there is no reaction. But if anything happens in Turkey that hurts European feelings, everybody says Turkey is not a reliable partner,” he said.

“It’s always been a marriage of convenience” — James Jeffrey, former U.S. ambassador to Turkey

Given that NATO member countries had imposed arms embargoes on Turkey in the past, Oymen said he saw Ankara’s attempts to buy Russian S-400 missile systems from Moscow as perfectly justifiable. NATO, however, is watching the negotiations with concern.

Such a purchase would be “unprecedented,” said Carnegie Europe’s Pierini, and not only because Turkey is firmly integrated in NATO’s own missile defence system. “A deal would be a major diplomatic coup for Moscow.”

He added: “Systems like these are not one-off purchases. They come with officers, training, longtime technical assistance. You’d basically implant top-notch Russian military officers in Turkey’s — a NATO country’s — defence structure.”

Marriage of convenience

Aside from the quarrels with its European allies, Ankara hasn’t endeared itself to the Americans either. The two NATO members are at odds in Syria, where Turkey has bombarded the YPG, a Kurdish militia it regards as a terrorist group threatening its territorial integrity. Yet for the U.S.-led coalition, the YPG is an integral partner in the fight against ISIS.

Erdoğan’s hopes for a reset in Turkish-American relations under Donald Trump, the only Western leader to congratulate him after his controversial referendum victory, were dashed when the new administration approved a plan to arm the YPG. Earlier this year, Turkish ministers threatened to deny the U.S. access to Incirlik, which would derail the fight against ISIS.

The Turkish leader’s recent Washington visit did nothing to boost ties, turning into a public relations nightmare after Erdoğan’s armed bodyguards were filmed beating up protesters outside the Turkish embassy.

Yet NATO will tolerate Erdoğan’s actions as long as they do not undercut the alliance’s central principles, said James Jeffrey, a former U.S. ambassador to Turkey and fellow at the Washington Institute, a think tank.

“Turkey is an essential part of NATO. Everybody will overlook what he does, like having protesters beaten up in Washington or calling Germany a Nazi state, because Turkey is needed in these quasi-existential security challenges we have in this region,” Jeffrey said. “It’s always been a marriage of convenience.”

David Herszenhorn contributed to this article.