In 1999, Hugo Chavez, the newly elected president of Venezuela, threw the opening pitch at a Mets game at Shea Stadium. He slammed the gavel to close the day’s trading at the New York Stock Exchange. He visited the White House, where he was so excited to meet Bill Clinton that Clinton hardly got a word in edgewise.

So what went wrong? How did Chavez become such a thorn in the side of the United States?

To fully understand the answer, you have to understand a key factor that shaped the admiration and angst that Chavez felt toward America: oil. Indeed, ever since 1922 — when prospectors hit a vein of crude in a sleepy lakeside Venezuelan town — oil has shaped the way millions of Venezuelans have viewed the United States.

Venezuela’s oil industry, which accounts for nearly half of government revenue, was built by roughnecks from Texas, Louisiana, and Oklahoma. They brought baseball and Boy Scouts to Venezuela. But they also brought a Jim Crow mentality, according to Miguel Tinker Salas, a Pomona College professor who authored a book on the history of the oil industry in Venezuela. The Americans relaxed at whites-only golf courses and social clubs. They lived in gated communities with servants and manicured lawns. Meanwhile, Venezuelan villages sprung up on the other side of the razor wire, populated by people seeking work as day laborers in the oil fields, or in bars and brothels that catered to Americans.

Oil became the only ticket to prosperity. It made Venezuela one of the wealthiest countries in Latin America, but also one of the most unequal. In the 1950s, when Chavez was so poor that he was kicked out of school for lack of a pair of shoes, oil wealth built lavish hotels in Caracas, and a Christian Dior boutique.

For much of the last century, the US government didn’t care whether the leader of Venezuela was a dictator or a democrat. All that mattered was that the oil kept flowing. The CIA worked with Venezuelan strongmen to repress labor unions and communists in the oil fields.

Because of oil, Venezuelan leaders didn’t have to invest wisely or develop new industries. They stayed in power not because they governed well but because they doled out the oil profits to the right group of people.

When oil prices were high, the money was enough to support a growing middle class, which was replacing Americans in the oil industry. As democracy took root in the 1970s, Venezuela nationalized its oil. But Venezuela paid off the oil companies, and kept up good relations. Venezuelan middle managers kept the oil flowing.

That worked until the 1990s, when oil prices collapsed. Revenue plummeted. Venezuela’s government cut back spending. Unrest spread. That’s how Chavez came to power. He pledged to “democratize” the oil industry and spend the profits on the poor, who had never benefited from oil before.

But a coup against Chavez took place in 2002, after he replaced the board of directors at the state-run oil company. The industry revolted, and called for a series of strikes that stopped the flow of oil for the first time in history. A junta took power, and named as president Pedro Carmona, the former manager of a petrochemical company.

It’s no accident that George W. Bush, from oil-producing Texas, supported Carmona, even as Carmona nullified the constitution and dissolved the National Assembly. Carmona seemed more capable of keeping the oil flowing than the soldiers whom Chavez wanted to put in charge.

The coup collapsed when the poor poured into the streets to demand Chavez’s reinstatement. Chavez returned to power and fired 20,000 oil workers. He traveled the world railing against the United States.

US officials returned the favor. But many acknowledged that Chavez wasn’t the root of the problem. It was the politics of oil, which have proven poisonous all over the world.

“People who think the problems of Venezuela are due to Hugo Chavez don’t understand that he was a symptom of the collapse of. . . a model that is based on the distribution of oil wealth,” said Arturo Valenzuela, a former senior official under Presidents Clinton and Obama.

Chavez “got his hands on the faucet that produces the wealth,” Valenzuela said. “His detractors wanted to get rid of him so they could get their hands back on that faucet.”

Chavez is dead now. But unfortunately, the “oil-ocracy’’ that kept him — and his predecessors — in power remains alive and well.