In the ArcGIS Python API you can use something as follows,
gis = GIS("https://domain.com.sa/arcgis", client_id='<your-app-id>')
print("Successfully logged in as: " + gis.properties.user.username)
You'll initially see the follwoing output
Please sign in to your GIS and paste the code that is obtained below.
If a web browser does not automatically open, please navigate to the URL below yourself instead.
Opening web browser to navigate to: https://domain.com/arcgis/sharing/rest/oauth2/authorize?response_type=code&client_id=<your-app-id>&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&state=<some-random-code>&allow_verification=false
You will then be redirected to the website with the code once you are succesfully authorized. Copy that code and paste it into the input windows and hit enter. You'll then see the following message
Successfully logged in as: user@domain.com
Abdur Rahman
GIS Developer