Switching the Wi-Fi network from terminal does not succeed (DHCP fails)
If you look at the usage for -setairportnetwork it implies that you have to include the password as an argument as well:

$ networksetup | grep setairportnetwork
Usage: networksetup -setairportnetwork &lt;device name&gt; &lt;network&gt; [password]


WiFi passwords are typically stored in Keychain so it's likely the case that the networking widget is pulling the password from Keychain and passing it as part of this operation behind the scenes.


How to find a Wi-Fi password on Mac


Example

Here I've looked in Keychain prior to get a rough idea of the item I'm attempting to retrieve via the CLI.



So we're looking for an item that contains "802.1X Password" for the "Kind". So we can use the security CLI tool to retrieve this like so:

$ security find-generic-password -D "802.1X Password" -g
keychain: "/Users/joeuser/Library/Keychains/login.keychain-db"
version: 512
class: "genp"
attributes:
    0x00000007 &lt;blob&gt;="SomeSSID Secure"
    0x00000008 &lt;blob&gt;=&lt;NULL&gt;
    "acct"&lt;blob&gt;="joeuser"
    "cdat"&lt;timedate&gt;=0x32303139303330343132323630355A00  "20190304122605Z\000"
    "crtr"&lt;uint32&gt;=&lt;NULL&gt;
    "cusi"&lt;sint32&gt;=&lt;NULL&gt;
    "desc"&lt;blob&gt;="802.1X Password"
    "gena"&lt;blob&gt;=&lt;NULL&gt;
    "icmt"&lt;blob&gt;=&lt;NULL&gt;
    "invi"&lt;sint32&gt;=&lt;NULL&gt;
    "mdat"&lt;timedate&gt;=0x32303139303330343132323630355A00  "20190304122605Z\000"
    "nega"&lt;sint32&gt;=&lt;NULL&gt;
    "prot"&lt;blob&gt;=&lt;NULL&gt;
    "scrp"&lt;sint32&gt;=&lt;NULL&gt;
    "svce"&lt;blob&gt;="com.apple.network.eap.user.item.wlan.ssid.SomeSSID Secure"
    "type"&lt;uint32&gt;=&lt;NULL&gt;
password: "securitypasswordssid123"


NOTE: When you run this command, you'll still be prompted for passwords to allow security CLI to access the Keychain. You can either do this 1 time, or allow it indefinitely.

The dialog will look like this:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

References


How to find a Wi-Fi password on Mac
How to Find Wi-Fi Network Passwords from Command Line on Mac
802.1X Management on the command line