I'm making a QGIS plugin in which I ask the for authentication object (with QgsProcessingParameterAuthConfig) for the PostgreSQL connection (which is already set in the connecions list of the user). My goal is to take the login and password with PyQGIS and use these to connect with psycopg2.
The asked parameter QgsProcessingParameterAuthConfig returns a string with the identification key of the authentication object.
So it seems like you do the following with the string (id):
# get the config id as a string
auth_method_id = self.parameterAsString(
parameters,
self.AUTH_CONFIG,
context
)
# get the application's authenticaion manager
auth_mgr = QgsApplication.authManager()
# create an empty authmethodconfig object
auth_cfg = QgsAuthMethodConfig()
# load config from manager to the new config instance and decrypt sensitive data
auth_mgr.loadAuthenticationConfig(auth_method_id, auth_cfg, True)
# get the configuration information (including username and password)
auth_cfg.configMap()
I got this from various places in documentation:
https://qgis.org/pyqgis/master/core/QgsAuthManager.html
https://qgis.org/pyqgis/master/core/QgsAuthMethodConfig.html
https://qgis.org/pyqgis/master/core/QgsProcessingParameterAuthConfig.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With