Hungary's foreign minister, Peter Szijjarto, called the court decision "outrageous and irresponsible".
The two nations opposed a 2015 decision by the EU's top policy body, at the height of the Mediterranean migration crisis, to assist Italy and Greece by making other European Union states admit 120,000 people.
Hungary asked Brussels last week to pay €400 million towards the cost of security at its frontiers, and Mr Szijjarto complained that "the European Commission is not funding border protection, but only the admission of illegal immigrants".
"Politics raped European law", Szijjarto told reporters in Budapest, adding that the decision established the power of the European Commission over the member states. He said it jeopardizes the security and future of Europe.
The Hungarian Helsinki Committee, an advocate for asylum-seekers, urged Hungary to give refugees an opportunity to make their case for asylum.
Under global and European law, countries are required to grant asylum to people fleeing war or persecution but not those classed as economic migrants, the EU designation for most sub-Saharan Africans.
Since 2014 around 1.7 million people have travelled to the European Union with the intention of settling here, with many fleeing wars or strife in the Middle East and Africa.
Amnesty International welcomed the court's decision.
Beata Szydlo, the Polish prime minister, said: 'We expected this decision. The European Court of Justice rejected a challenge by Hungary and Slovakia on September 6, 207, to stay out of a European Union scheme meant to relocate refugees.
In other words, the quotas don't work well as an important expression of political solidarity between European Union member states.
Budapest announced at the end of August that it will extend the state of emergency declared previous year over the migrant crisis until March 2018, while Hungarian government spokesman Zoltan Kovacs said that the "threat of terrorism in Europe has increased", thus justifying increased border controls, according to The Associated Press. That measure, including payments of up to €6 billion to help Turkey manage its refugee population and a pledge to reinvigorate talks on the possibility of Turkey joining the bloc, slowed the flow across the Aegean route to a trickle.
What You Should Know About Apple's Upcoming iPhone 8
Bloomberg , ahead of the all but certain September debut of the iPhone 8 , has a breakdown of how it'll run . Next year, though, Apple is reportedly planning to launch three iPhone models in the coming iPhone cycle .
Diana aura still captivates, 20 years on
It is a more muted affair than in 1997, when a sea of flowers formed outside the gates after Diana's death. Second in line to the throne William said: "She was this ray of light in a fairly grey world".
Senior journalist Gauri Lankesh shot dead in Bengaluru
She demanded that Prime Minister Narendra Modi should look into the matter and provide answers for the attacks. Investigators pinned their hopes on the CCTV footage to get some clues about the identity of the killers.
Canadian economy beats expectations with 4.5% GDP growth in Q2
Some are now saying that the devastation from Hurricane Harvey could shave about a half-percentage point off growth this quarter. This second estimate is much better than the first (+2.6 per cent), is higher than the expectations of analysts (2.7 per cent).