Pan*_*nda 3 azure azure-active-directory azure-sql-database
目前,我使用设备代码凭据来访问 Azure AD。
device_code_credential = DeviceCodeCredential(
azure_client_id,
tenant_id=azure_tenant_id,
authority=azure_authority_uri)
Run Code Online (Sandbox Code Playgroud)
但我仍然需要使用 Azure 帐户用户名/密码来连接到 Azure SQL 服务器
driver = 'ODBC Driver 17 for SQL Server'
db_connection_string = f'DRIVER={driver};SERVER={server};' \
f'DATABASE={database};UID={user_name};PWD={password};'\
f'Authentication=ActiveDirectoryPassword;'\
'Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;'
connector = pyodbc.connect(db_connection_string)
Run Code Online (Sandbox Code Playgroud)
linux/MacOS 下的 python 有什么方法可以允许我使用 device_code_credential 和 access_token 连接到 Azure SQL 服务器吗?
https://github.com/mkleehammer/pyodbc/issues/228
我只得到了这个链接,但似乎不起作用。
有人有完整的工作样本吗?
您可以参考本教程:AzureAD/azure-activedirectory-library-for-python:连接到 Azure SQL 数据库。
可以通过 ADAL Python 从 Azure Active Directory (AAD) 获取令牌来连接到 Azure SQL 数据库。我们目前没有维护它的完整示例,但本文概述了一些关键要素。
https://database.windows.net/资源字符串。请注意,您需要保留尾部斜杠,否则发行的令牌将不起作用。这适用于 AAD 访问令牌。在 Python 2.x 中扩展令牌并添加长度的示例代码,如上面链接的页面中所述:
token = "eyJ0eXAiOi...";
exptoken = "";
for i in token:
exptoken += i;
exptoken += chr(0);
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken;
conn = pyodbc.connect(connstr, attrs_before = { 1256:bytearray(tokenstruct) });
Run Code Online (Sandbox Code Playgroud)
由于烦人的字符/字节分割,3.x 只是稍微复杂一些:
token = b"eyJ0eXAiOi...";
exptoken = b"";
for i in token:
exptoken += bytes({i});
exptoken += bytes(1);
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken;
conn = pyodbc.connect(connstr, attrs_before = { 1256:tokenstruct });
Run Code Online (Sandbox Code Playgroud)
(SQL_COPT_SS_ACCESS_TOKEN 为 1256;它特定于 msodbcsql 驱动程序,因此 pyodbc 没有定义它,而且很可能也不会。)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
6960 次 |
| 最近记录: |