3 Answers
3

The OED gives a 1574 quote for spill it meaning 'to divulge, let out': "Although it be a shame to spill it, I will not leaue ['omit'] to say that which..his friends haue said vnto me."

The spilling of beans endures, within and outside the U.S. (...) Over the years, there have been countless variations of form and meaning, all with spill. (...) And although we have tracked spill, we still don't know beans.

A popular folk etymology for spill the beans claims that in ancient Greece, applicants for membership in secret societies were voted upon by having the existing members drop beans into an opaque pottery jar.

Etymonline says that this is a rather recent phrase, first recorded in 1919. The Phrase Finder supplies two slightly older quotes.

The earliest uses of 'spill the beans' come from the USA. The meaning of the phrase was then something like 'spoil the beans' or 'upset the applecart'[.] The first example I can find is from The Stevens Point Journal, June 1908 [.]

Soon after that the phrase was used with the meaning of 'upset a previously stable situation by talking out of turn', which is close to how we use it today. That is cited in The Van Wert Daily Bulletin, October 1911:

Finally Secretary Fisher, of the President's cabinet, who had just returned from a trip to Alaska, was called by Governor Stubbs to the front, and proceeded, as one writer says, to 'spill the beans'.

Note the quotation marks, which suggest that this wasn't an established phrase back then.

Summary

Spill the beans was used in horse-racing in St. Louis, MO, as early as 1902 and meant "to cause an upset". By 1907 it was being used with a similar meaning in baseball in other states.

1902 horse-racing

The OED has spill the beans meaning "to reveal a secret" from 1919. The Phrase Finder has 1908 for the meaning of "upset the applecart" and 1911 for "upset a previously stable situation by talking out of turn".

The earliest I found is a 1902 beans were spilled in horse-racing, meaning "caused an upset", quoting owner E. J. Arnold.

"Ethylene was 15 to 1 one day and would
have won sure had Battiste been up. He
was set down by the starter in St. Louis
and I had to ride a bad boy. Then we put
Battiste up later and got down. Kiley told
him to take her back a bit. He did, and
in some manner the field ran around and
over her so that she was shut in, cut off
and lost. So the beans were spilled.

1903 horse-racing

I found some 1903 examples, all from horse-racing meaning to cause an upset, and all in the same The St. Louis Republic.

Every one fancied that the fifth race was
a two-horse one between Nearest and Audiphone, who were held at 4 to 5 and 8 to 5
respectively. Kingstelle, a 10-to-l shot,
broke it up. She laid away from the pace
and came along in the stretch, and won,
handily, a real nice race. Nearest and
Audiphone ran to a head finish. Nearest
getting the best of it. It was the third
time that these two horses met on equal
terms. Each time Nearest has beaten Audiphone by a head. This is form of a miraculous nature. The secret of it is that both
have been trying hard. Neither Mr. Flippen, who trains Audiphone, or Mr. Hughes,
who owns Nearest, is around stalling with
the old complaint: "Now, you know you
feel good one day and bad the next. That
is just like a horse."

1907 baseball

At
this point the game began to get
interesting, as Logan was just two
scores behind, and were beginning
to find Farrow's delivery with
ease, but the beans were spilled in
the eighth when Jones, who played
sensational ball all during the
game, erred on Murphy and Kelley, first two up, this followed by
stolen base and an error by Johnson at the plate and Farrow's
single were good for thee more
runs, bringing their total to nine,
just three more than Logan made
during the entire game.

And beyond

As The Phrase Finder shows by 1908 it was used outside sport meaning "to cause an upset". And by 1911 it was used in politics meaning "upset a previously stable situation by talking out of turn". World Wide Words has a 1910 "extension of the sports sense into upsetting a situation by speaking out". From there, the meaning evolved to the modern one, to reveal a secret which may cause an upset.

'Spill the beans' has a slightly different meaning in today's usage from 'upset the apple cart'. The former means to revel accidentally a secret. The latter is less specific and means to say or do something that upsets a pre-arranged plan.
–
WS2Jun 1 '14 at 9:15