The federal law explicitly permits states and localities to claim immunity from drug laws for their officers while engaged in officially sanctioned acts. As per the snippet I posted above, this is to prevent things like narcotics officers being prosecuted when they do things like go undercover to buy and sell drugs from traffickers. The City of Oakland decided that Rosenthal was such an officer, engaging in officially sanctioned activity, and hence was entitled to immunity for his acts.
If he was so smart why didn't he know that they did not have the authority?
Rosenthal believed in good faith that the officials of the city of Oakland had the authority to do what they did. The officials of the city of Oakland believed in good faith that the federal law permitted them to do what they did. Until Rosenthal was arrested, nobody from the federal government apparently bothered to tell them otherwise.
This was no sting operation as you have described it. It was possession for distribution.
Cops cannot do that and the city cannot authorize it. Only doctors and pharmasists with authorization by way of a federal authorization can distribute controlled substances. I will bet that all your cops could have told them that.