I want to create a new authentication configuration and a new connection to the Oracle database programmatically. I successfully create both of them; however, in the end, the username and password are still saved as plain text rather than being stored using authcfg. What am I missing?
# https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/authentication.html#populate-authdb-with-a-new-authentication-configuration-entry
auth_mgr = QgsApplication.authManager()
auth_config = QgsAuthMethodConfig()
auth_config.setName('myconfig')
auth_config.setMethod('Basic')
auth_config.setConfigMap({
'username': login,
'password': password
})
assert auth_config.isValid()
auth_mgr.storeAuthenticationConfig(auth_config)
auth_config_id = auth_config.id()
assert auth_config_id
# https://gis.stackexchange.com/a/461589/173206
uri = QgsDataSourceUri()
uri.setConnection('myhost', '1521', 'mydb', None, None, QgsDataSourceUri.SslPrefer, auth_config_id)
uri.setSchema('myschema')
conn_config = {
"saveUsername": True,
"savePassword": True,
"estimatedMetadata": True,
"metadataInDatabase": True,
"onlyExistingTypes": False,
}
metadata = QgsProviderRegistry.instance().providerMetadata('oracle')
connection = metadata.createConnection(uri.uri(False), conn_config) # tried both True and False inside uri()
connection: QgsAbstractDatabaseProviderConnection
connection.store('My Connection')
iface.browserModel().reload()
Then I drag and drop a layer from this connection to the map. In the end, the source of the layer from this connection looks like this:
iface.activeLayer().source()
'dbname=\'mydb\' host=myhost port=1521 user=\'username\' password=\'topsecret\' srid=4326 type=Polygon table="myschema"."mylayer" (SHAPE)'
I want it to look like this (this is from a connection created using the GUI):
iface.activeLayer().source()
'dbname=\'mydb\' host=myhost port=1521 authcfg=vnh3ijp estimatedmetadata=true srid=4326 type=Polygon table="myschema"."mylayer" (SHAPE)'
The problem is that properties of the connection created by this code looks like this:
There is no authentication method selected despite creating it - but it appears in the list when expanded, and it's also visible in Settings -> Options -> Authentication. Also, why is there no schema set, despite using uri.setSchema('myschema')?

expandAuthConfigparameter in theuri()method isTrue, so tryconnection = metadata.createConnection(uri.uri(False), conn_config). See docs.qgis.org/3.40/en/docs/pyqgis_developer_cookbook/…, where it says "TheFalseargument passed touri.uri(False)prevents the expansion of the authentication configuration parameters, if you are not using any authentication configuration this argument does not make any difference".Falseargument to theuri()method didn't help.source()of the layer consists of the username and password as plain text. The code runs without any errors.publicSource(), that could theoretically help