我正在制作一个QGIS插件,我向PostgreSQL连接(要求已在用户的连接列表中设置)请求身份验证对象(使用QgsProcessingParameterAuthConfig)。我的目标是使用PyQGIS获取登录名和密码,并使用它们与psycopg2连接。
谢谢!
询问的参数QgsProcessingParameterAuthConfig返回带有认证对象的标识密钥的字符串。
因此,似乎您要对字符串(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()
我从文档的不同地方得到了此:
https://qgis.org/pyqgis/master/core/QgsAuthManager.html
https://qgis.org/pyqgis/master/core/QgsAuthMethodConfig.html
https://qgis.org/pyqgis/master/core/QgsProcessingParameterAuthConfig.html