<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Databricks SQL connectivity in Python with Service Principals in Administration &amp; Architecture</title>
    <link>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67232#M1108</link>
    <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/83912"&gt;@harripy&lt;/a&gt;&amp;nbsp;, could you please brief us about your use case? So that we can understand the issue better.&lt;/P&gt;</description>
    <pubDate>Thu, 25 Apr 2024 06:23:42 GMT</pubDate>
    <dc:creator>Yeshwanth</dc:creator>
    <dc:date>2024-04-25T06:23:42Z</dc:date>
    <item>
      <title>Databricks SQL connectivity in Python with Service Principals</title>
      <link>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67052#M1096</link>
      <description>&lt;P&gt;Tried to use M2M OAuth connectivity on Databricks SQL Warehouse in Python:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;from databricks.sdk.core import Config, oauth_service_principal
from databricks import sql

....
config = Config(host=f"https://{host}",
                                client_id=db_user,
                                client_secret=passwd)
oauth_sp = oauth_service_principal(config)
return sql.connect(server_hostname=host,
                   http_path=http_path,
                   catalog=catalog,
                   schema=schema,
                   credentials_provider=oauth_sp)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp; The params mentioned are properly assigned, but this fails to an error:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Traceback (most recent call last):
  File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;
  File "/path/venv/lib/python3.11/site-packages/databricks/sql/__init__.py", line 50, in connect
    return Connection(server_hostname, http_path, access_token, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/venv/lib/python3.11/site-packages/databricks/sql/client.py", line 189, in __init__
    self._session_handle = self.thrift_backend.open_session(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/venv/lib/python3.11/site-packages/databricks/sql/thrift_backend.py", line 464, in open_session
    response = self.make_request(self._client.OpenSession, open_session_req)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/venv/lib/python3.11/site-packages/databricks/sql/thrift_backend.py", line 393, in make_request
    self._handle_request_error(error_info, attempt, elapsed)
  File "/path/venv/lib/python3.11/site-packages/databricks/sql/thrift_backend.py", line 261, in _handle_request_error
    raise network_request_error
databricks.sql.exc.RequestError: Error during request to server&lt;/LI-CODE&gt;&lt;P&gt;The credentials provider seems to be set correctly, yet the connection is tried to be established with the access_token - what am I missing?&lt;/P&gt;</description>
      <pubDate>Tue, 23 Apr 2024 11:46:15 GMT</pubDate>
      <guid>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67052#M1096</guid>
      <dc:creator>harripy</dc:creator>
      <dc:date>2024-04-23T11:46:15Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks SQL connectivity in Python with Service Principals</title>
      <link>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67232#M1108</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/83912"&gt;@harripy&lt;/a&gt;&amp;nbsp;, could you please brief us about your use case? So that we can understand the issue better.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 06:23:42 GMT</pubDate>
      <guid>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67232#M1108</guid>
      <dc:creator>Yeshwanth</dc:creator>
      <dc:date>2024-04-25T06:23:42Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks SQL connectivity in Python with Service Principals</title>
      <link>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67236#M1110</link>
      <description>&lt;P&gt;We have configured a service principal for Databricks SQL Warehouse and we want to do DML calls on the database through Python connection (mostly INSERT-SELECT-clauses). I found documentation stating that it &lt;STRONG&gt;&lt;EM&gt;SHOULD&lt;/EM&gt;&lt;/STRONG&gt; be possible to get the connection to the SQL Warehouse just by providing the m2m-auth configuration (having the databricks sp client id and databricks sp client secret) as the credentials_provider. During execution the error message though indicates that the connection is tried to be opened with access token instead of the m2m authentication. I might be missing out some crucial configuration somewhere, but it would be nice to get this working.&lt;/P&gt;&lt;P&gt;Current working solution is for us:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;connect to workspace with sp client/secret&lt;/LI&gt;&lt;LI&gt;request token from workspace&lt;/LI&gt;&lt;LI&gt;connect using the given token&lt;/LI&gt;&lt;LI&gt;on connection close delete the token&lt;/LI&gt;&lt;/OL&gt;</description>
      <pubDate>Thu, 25 Apr 2024 06:40:11 GMT</pubDate>
      <guid>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67236#M1110</guid>
      <dc:creator>harripy</dc:creator>
      <dc:date>2024-04-25T06:40:11Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks SQL connectivity in Python with Service Principals</title>
      <link>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67241#M1111</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/83912"&gt;@harripy&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;I've just looked through the Databricks SDK code and it looks like m2m auth is generating oauth bearer token to authenticate, that's why you see "access_token", but it's different from PAT (&lt;A href="https://github.com/databricks/databricks-sdk-py/blob/68feadf82f12d387621704f2957d743bfc86f4bd/databricks/sdk/oauth.py#L413" target="_blank"&gt;https://github.com/databricks/databricks-sdk-py/blob/68feadf82f12d387621704f2957d743bfc86f4bd/databricks/sdk/oauth.py#L413&lt;/A&gt;).&lt;BR /&gt;&lt;BR /&gt;So it looks like it's able to authenticate with Databricks through M2M and get access token.&amp;nbsp;&lt;BR /&gt;It's hard to guess what's the issue with the logs that you've provided.&lt;BR /&gt;&lt;BR /&gt;What i suggest is to enable debug logging and paste the logs.&lt;BR /&gt;&lt;A href="https://github.com/databricks/databricks-sdk-py/tree/68feadf82f12d387621704f2957d743bfc86f4bd?tab=readme-ov-file#logging" target="_self"&gt;https://github.com/databricks/databricks-sdk-py/tree/68feadf82f12d387621704f2957d743bfc86f4bd?tab=readme-ov-file#logging&lt;/A&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 08:45:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67241#M1111</guid>
      <dc:creator>daniel_sahal</dc:creator>
      <dc:date>2024-04-25T08:45:56Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks SQL connectivity in Python with Service Principals</title>
      <link>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67372#M1120</link>
      <description>&lt;P&gt;alright, made following test call:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;dbConfig = {'targetDvUser':db_user,
            'targetDvPassword':password, 
            'targetDvHost':'dbc-098946cd-8b3a.cloud.databricks.com',
            'targetDvCatalog':'ade_test',
            'targetHttpPath':'/sql/1.0/warehouses/7b15eacc3fef2e55',
            'targetSchema':'manager_test_rdv'}

import logging,sys
from databricks.sdk.core import Config, oauth_service_principal
from databricks import sql
from databricks.sql.exc import Error, OperationalError

logging.basicConfig(stream=sys.stderr,
                    level=logging.INFO,
                    format='%(asctime)s [%(name)s][%(levelname)s] %(message)s')
logging.getLogger('databricks.sdk').setLevel(logging.DEBUG)

config = Config(host=f"https://{dbConfig['targetDvHost']}",
                                client_id=dbConfig['targetDvUser'],
                                client_secret=dbConfig['targetDvPassword'])
oauth_sp = oauth_service_principal(config)
conn = sql.connect(server_hostname=dbConfig['targetDvHost'],
                   http_path=dbConfig['targetHttpPath'],
                   catalog=dbConfig['targetDvCatalog'],
                   schema=dbConfig['targetSchema'],
                   credentials_provider=oauth_sp)&lt;/LI-CODE&gt;&lt;P&gt;And the response was as follows:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;gt;&amp;gt;&amp;gt; dbConfig = {'targetDvUser':db_user,
...             'targetDvPassword':password,
...             'targetDvHost':'dbc-098946cd-8b3a.cloud.databricks.com',
...             'targetDvCatalog':'ade_test',
...             'targetHttpPath':'/sql/1.0/warehouses/7b15eacc3fef2e55',
...             'targetSchema':'manager_test_rdv'}
&amp;gt;&amp;gt;&amp;gt; import logging,sys
&amp;gt;&amp;gt;&amp;gt; from databricks.sdk.core import Config, oauth_service_principal
&amp;gt;&amp;gt;&amp;gt; from databricks import sql
&amp;gt;&amp;gt;&amp;gt; from databricks.sql.exc import Error, OperationalError
&amp;gt;&amp;gt;&amp;gt; logging.basicConfig(stream=sys.stderr,
...                     level=logging.INFO,
...                     format='%(asctime)s [%(name)s][%(levelname)s] %(message)s')
&amp;gt;&amp;gt;&amp;gt; logging.getLogger('databricks.sdk').setLevel(logging.DEBUG)
&amp;gt;&amp;gt;&amp;gt; config = Config(host=f"https://{dbConfig['targetDvHost']}",
...                                 client_id=dbConfig['targetDvUser'],
...                                 client_secret=dbConfig['targetDvPassword'])
2024-04-26 12:37:43,611 [databricks.sdk][DEBUG] Attempting to configure auth: pat
2024-04-26 12:37:43,611 [databricks.sdk][DEBUG] Attempting to configure auth: basic
2024-04-26 12:37:43,611 [databricks.sdk][DEBUG] Attempting to configure auth: metadata-service
2024-04-26 12:37:43,611 [databricks.sdk][DEBUG] Attempting to configure auth: oauth-m2m
&amp;gt;&amp;gt;&amp;gt; oauth_sp = oauth_service_principal(config)
&amp;gt;&amp;gt;&amp;gt; conn = sql.connect(server_hostname=dbConfig['targetDvHost'],
...                    http_path=dbConfig['targetHttpPath'],
...                    catalog=dbConfig['targetDvCatalog'],
...                    schema=dbConfig['targetSchema'],
...                    credentials_provider=oauth_sp)
2024-04-26 12:38:22,632 [databricks.sdk.oauth][DEBUG] Retrieving token for f15f66cb-d173-432b-b530-896d2834d4c7
2024-04-26 12:38:22,967 [databricks.sql.thrift_backend][INFO] Error during request to server: {"method": "OpenSession", "session-id": null, "query-id": null, "http-code": null, "error-message": "", "original-exception": "'dict' object is not callable", "no-retry-reason": "non-retryable error", "bounded-retry-delay": null, "attempt": "1/30", "elapsed-seconds": "0.00017309188842773438/900.0"}
Traceback (most recent call last):
  File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;
  File "/Users/harri.pylkkanen/workspace/dv-executor/venv/lib/python3.11/site-packages/databricks/sql/__init__.py", line 50, in connect
    return Connection(server_hostname, http_path, access_token, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/harri.pylkkanen/workspace/dv-executor/venv/lib/python3.11/site-packages/databricks/sql/client.py", line 189, in __init__
    self._session_handle = self.thrift_backend.open_session(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/harri.pylkkanen/workspace/dv-executor/venv/lib/python3.11/site-packages/databricks/sql/thrift_backend.py", line 464, in open_session
    response = self.make_request(self._client.OpenSession, open_session_req)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/harri.pylkkanen/workspace/dv-executor/venv/lib/python3.11/site-packages/databricks/sql/thrift_backend.py", line 393, in make_request
    self._handle_request_error(error_info, attempt, elapsed)
  File "/Users/harri.pylkkanen/workspace/dv-executor/venv/lib/python3.11/site-packages/databricks/sql/thrift_backend.py", line 261, in _handle_request_error
    raise network_request_error
databricks.sql.exc.RequestError: Error during request to server&lt;/LI-CODE&gt;&lt;P&gt;So indeed the&amp;nbsp;&lt;SPAN&gt;oauth bearer token is generated (or being generated), but then the open session crashes - is this a service principal permissions related thing or some configuration missed somewhere?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 09:43:32 GMT</pubDate>
      <guid>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67372#M1120</guid>
      <dc:creator>harripy</dc:creator>
      <dc:date>2024-04-26T09:43:32Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks SQL connectivity in Python with Service Principals</title>
      <link>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67374#M1121</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/83912"&gt;@harripy&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Looking at the SDK logs - i've got no idea what could happen here. I think that SQL logs could be handy. Could you set databricks.sql logging level to DEBUG as well?&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 10:24:09 GMT</pubDate>
      <guid>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67374#M1121</guid>
      <dc:creator>daniel_sahal</dc:creator>
      <dc:date>2024-04-26T10:24:09Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks SQL connectivity in Python with Service Principals</title>
      <link>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67524#M1129</link>
      <description>&lt;P&gt;These log lines will be added with databricks.sql DEBUG:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;2024-04-29 08:47:10,276 [databricks.sql.thrift_backend][DEBUG] retry parameter: _retry_delay_min given_or_default 1.0
2024-04-29 08:47:10,277 [databricks.sql.thrift_backend][DEBUG] retry parameter: _retry_delay_max given_or_default 60.0
2024-04-29 08:47:10,277 [databricks.sql.thrift_backend][DEBUG] retry parameter: _retry_stop_after_attempts_count given_or_default 30
2024-04-29 08:47:10,277 [databricks.sql.thrift_backend][DEBUG] retry parameter: _retry_stop_after_attempts_duration given_or_default 900.0
2024-04-29 08:47:10,277 [databricks.sql.thrift_backend][DEBUG] retry parameter: _retry_delay_default given_or_default 5.0
2024-04-29 08:47:10,309 [databricks.sql.thrift_backend][DEBUG] Sending request: TOpenSessionReq(client_protocol=None, username=None, password=None, configuration={'spark.thriftserver.arrowBasedRowSet.timestampAsString': 'false'}, getInfos=None, client_protocol_i64=42247, connectionProperties=None, initialNamespace=TNamespace(catalogName='ade_test', schemaName='manager_test_rdv'), canUseMultipleCatalogs=True, sessionId=None)
2024-04-29 08:47:10,310 [databricks.sql.thrift_backend][INFO] Error during request to server: {"method": "OpenSession", "session-id": null, "query-id": null, "http-code": null, "error-message": "", "original-exception": "'dict' object is not callable", "no-retry-reason": "non-retryable error", "bounded-retry-delay": null, "attempt": "1/30", "elapsed-seconds": "0.00023293495178222656/900.0"}
Traceback (most recent call last):&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 29 Apr 2024 05:49:53 GMT</pubDate>
      <guid>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/67524#M1129</guid>
      <dc:creator>harripy</dc:creator>
      <dc:date>2024-04-29T05:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks SQL connectivity in Python with Service Principals</title>
      <link>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/69098#M1198</link>
      <description>&lt;P&gt;I am facing the same issue with the same error logs as&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/83912"&gt;@harripy&lt;/a&gt;. Can you please help&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/32523"&gt;@Yeshwanth&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/46008"&gt;@Dani&lt;/a&gt;&amp;nbsp;?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2024 17:02:27 GMT</pubDate>
      <guid>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/69098#M1198</guid>
      <dc:creator>kavyakavuri</dc:creator>
      <dc:date>2024-05-15T17:02:27Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks SQL connectivity in Python with Service Principals</title>
      <link>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/92719#M1976</link>
      <description>&lt;P&gt;Did anyone get this to work? I have tried the code above but I get a slightly different error but I don't see the same level of details from the logs&lt;/P&gt;&lt;P&gt;2024-10-04 14:59:25,508 [databricks.sdk][DEBUG] Attempting to configure auth: pat&lt;BR /&gt;2024-10-04 14:59:25,508 [databricks.sdk][DEBUG] Attempting to configure auth: basic&lt;BR /&gt;2024-10-04 14:59:25,509 [databricks.sdk][DEBUG] Attempting to configure auth: metadata-service&lt;BR /&gt;2024-10-04 14:59:25,509 [databricks.sdk][DEBUG] Attempting to configure auth: oauth-m2m&lt;BR /&gt;2024-10-04 14:59:25,523 [urllib3.connectionpool][DEBUG] Starting new HTTPS connection (1): adb-xx.azuredatabricks.net:443&lt;BR /&gt;2024-10-04 14:59:25,706 [urllib3.connectionpool][DEBUG] &lt;A href="https://adb-xx.azuredatabricks.net:443" target="_blank"&gt;https://adb-xx.azuredatabricks.net:443&lt;/A&gt; "GET /oidc/.well-known/oauth-authorization-server HTTP/11" 200 None&lt;BR /&gt;2024-10-04 14:59:25,714 [urllib3.connectionpool][DEBUG] Starting new HTTPS connection (1): adb-xx.azuredatabricks.net:443&lt;BR /&gt;2024-10-04 14:59:25,871 [urllib3.connectionpool][DEBUG] &lt;A href="https://adb-xx.azuredatabricks.net:443" target="_blank"&gt;https://adb-xx.azuredatabricks.net:443&lt;/A&gt; "GET /oidc/.well-known/oauth-authorization-server HTTP/11" 200 None&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "H:\Documents\Git_New\DataHubUserAccess\source\Python\take3.py", line 28, in &amp;lt;module&amp;gt;&lt;BR /&gt;conn = sql.connect(server_hostname=dbConfig['targetDvHost'],&lt;BR /&gt;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&lt;BR /&gt;File "C:\Users\matcon\Python\Lib\site-packages\databricks\sql\__init__.py", line 50, in connect&lt;BR /&gt;return Connection(server_hostname, http_path, access_token, **kwargs)&lt;BR /&gt;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&lt;BR /&gt;File "C:\Users\matcon\Python\Lib\site-packages\databricks\sql\client.py", line 157, in __init__&lt;BR /&gt;auth_provider = get_python_sql_connector_auth_provider(&lt;BR /&gt;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&lt;BR /&gt;File "C:\Users\matcon\Python\Lib\site-packages\databricks\sql\auth\auth.py", line 96, in get_python_sql_connector_auth_provider&lt;BR /&gt;return get_auth_provider(cfg)&lt;BR /&gt;^^^^^^^^^^^^^^^^^^^^^^&lt;BR /&gt;File "C:\Users\matcon\Python\Lib\site-packages\databricks\sql\auth\auth.py", line 68, in get_auth_provider&lt;BR /&gt;raise RuntimeError("No valid authentication settings!")&lt;BR /&gt;RuntimeError: No valid authentication settings!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 05:04:02 GMT</pubDate>
      <guid>https://community.databricks.com/t5/administration-architecture/databricks-sql-connectivity-in-python-with-service-principals/m-p/92719#M1976</guid>
      <dc:creator>Mat_Conquest</dc:creator>
      <dc:date>2024-10-04T05:04:02Z</dc:date>
    </item>
  </channel>
</rss>

