<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hello everyone,</div><div><br></div><div>I am having trouble creating a new authentication configuration and connecting to the Oracle database using the Python API. The new authentication configuration and connection are created without errors, but in the end, the connection does not use the encrypted configuration. When I click on 'Edit connection,' the configuration is listed, but it is not properly applied to the connection. As a result, the layers from the database store the user's password in plain text. Additionally, the connection schema is not properly saved despite using setSchema() method.</div><div><br></div><div>
<pre><code><span># <a href="https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/authentication.html#populate-authdb-with-a-new-authentication-configuration-entry" target="_blank">https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/authentication.html#populate-authdb-with-a-new-authentication-configuration-entry</a></span>
auth_mgr = QgsApplication.authManager()
auth_config = QgsAuthMethodConfig()
auth_config.setName(<span>'myconfig'</span>)
auth_config.setMethod(<span>'Basic'</span>)
auth_config.setConfigMap({
    <span>'username'</span>: login,
    <span>'password'</span>: password
})
<span>assert</span> auth_config.isValid()
auth_mgr.storeAuthenticationConfig(auth_config)
auth_config_id = auth_config.<span>id</span>()
<span>assert</span> auth_config_id

<span># <a href="https://gis.stackexchange.com/a/461589/173206" target="_blank">https://gis.stackexchange.com/a/461589/173206</a></span>
uri = QgsDataSourceUri()
uri.setConnection(<span>'myhost'</span>, <span>'1521'</span>, <span>'mydb'</span>, <span>None</span>, <span>None</span>, QgsDataSourceUri.SslPrefer, auth_config_id)
uri.setSchema(<span>'myschema'</span>)

conn_config = {
  <span>"saveUsername"</span>: <span>True</span>,
  <span>"savePassword"</span>: <span>True</span>,
  <span>"estimatedMetadata"</span>: <span>True</span>,
  <span>"metadataInDatabase"</span>: <span>True</span>,
  <span>"onlyExistingTypes"</span>: <span>False</span>,
}

metadata = QgsProviderRegistry.instance().providerMetadata(<span>'oracle'</span>)
connection = metadata.createConnection(uri.uri(<span>False</span>), conn_config)  <span># tried both True and False inside uri()</span>
connection: QgsAbstractDatabaseProviderConnection
connection.store(<span>'My Connection'</span>)
iface.browserModel().reload()</code></pre><div><br></div><div><img src="cid:ii_mgys7up10" alt="uncorrect_conn_settings.png" width="562" height="465"><br></div><div><br></div><div><img src="cid:ii_mgys8cp21" alt="auth_config_list.png" width="132" height="149"><br></div><div><br></div>I don't have any issues creating the authentication configuration and connecting to the PostgreSQL database this way – it uses the authentication configuration correctly. The problem occurs when I try to connect to Oracle.<br>
I've tested the code on Windows with QGIS versions 3.34, 3.40, and 3.44, and also when creating a new user profile.</div><div>I've posted the question on <a href="https://gis.stackexchange.com/questions/495763/adding-new-authentication-configuration-and-connection-to-oracle-database-with-e" target="_blank">gis.stackexchange.com</a>, but I think this case might be a bit tricky.</div><div><br></div><div>I know that one possible solution is to set the connection parameters <a href="https://gis.stackexchange.com/a/451880/173206" target="_blank">by modifying QSettings/QgsSettings</a> - I've done it successfully this way. However, I'm curious to know what's wrong with creating the connection using the code above. Does anyone have any idea?</div><div><br></div><div><br></div><div>Best regards,</div><div>Maryla</div></div>
</div></div></div></div></div></div></div>