This is a great way to do it and it is what I do.
One thing to think about is line 5 above. I grab the ArcPro credentials instead using gis = GIS('pro'). We have smart cards so I can use that to login to Pro and it keeps the connection for 2 weeks. For us IT security would throw a fit if we hardcoded our username and password in there. This also may help if you have 2 factor auth.
Also using verify_cert=False is considered bad security and IT would not let us do that either.
I use this code to grab the cert.
import wincertstore
# Loop through the windows trusted certificate store and generate a CA Bundle File in a temporary location
ca_bundle_file = r''
with tempfile.NamedTemporaryFile(delete=False) as tf:
for storename in ("CA", "ROOT"):
with wincertstore.CertSystemStore(storename) as store:
for cert in store.itercerts(usage=wincertstore.SERVER_AUTH):
# Py v2
try:
tf.write(cert.get_pem().decode("ascii"))
# Py v3
except:
tf.write(bytes(cert.get_pem(),"ascii"))
tf.flush()
ca_bundle_file = tf.name
tf.close()
# Set the "REQUESTS_CA_BUNDLE" variable
os.environ["REQUESTS_CA_BUNDLE"]=ca_bundle_file
Hope that helps someone.