Starbucks has apologized to a Philadelphia police officer whose saga of being denied access to the restroom at one of the chain's Center City shops went viral.

A sergeant lashed out online after a worker at the 1301 Chestnut St. location wouldn't give him the key code to the bathroom because he wasn't a paying customer.

The unnamed sergeant's rant was posted to the Starbucks' Facebook page Friday by Joe Leighthardt, another Philadelphia police officer, and has been shared more than 17,600 times since.

A Starbucks spokeswoman wrote in an email Tuesday that the business had apologized: "We have personally apologized to the customer and look forward to welcoming him back to our store."

After the officer, who was in full uniform, asked the "young blonde liberal behind the counter" for the restroom key code, "she states in a loud voice, so all the other customers can hear that the bathroom is for paying customers only," the post reads. "I then ask in a very polite manner if I could please use it. She then states in the same loud manner and a smirk 'Are you a paying customer?' "

The post adds that as he walked out of the Starbucks, the barista "continued loudly to tell me about the bathroom down the street" and "the many customers and employees said nothing and seemed indifferent" as the exchange unfolded.

The Starbucks spokeswoman said the officer's "experience was not consistent with the welcoming and friendly environment we strive to create for everyone."

In his post on Starbucks' Facebook page, Leighthardt noted that the store frequently calls police "for some sort of service."

The sergeant's original post also lamented how the public treats police officers.

"This is the world cops live in anymore," the post said. "It's hip for this generation to berate and totally disrespect cops in front of the public and praise cop killers as the heroes."

But after the officer's Starbucks ordeal went viral on Facebook, many users on the social media site said they would stop patronizing the coffee shop, and other Philadelphia business owners posted that officers were welcome to use their restrooms.