[英]How do you get SQL Token authentication working within an Azure Python 3 Runbook?
I have a Python 3 script in a Azure Runbook which I need to connect to a Azure SQL Server Database using Token Authentication.
問題是 {ODBC Driver 17 for SQL Server} 沒有預裝在 Azure 機器上,我們無法控制它。 我確實嘗試查看是否可以使用 os.system('msiexec') 將 {ODBC Driver 17 for SQL Server} 強制到盒子上,但這不起作用,因為無法訪問 Windows 安裝程序服務。
我不相信我的代碼有什么問題,因為這在我的筆記本電腦上運行良好。 我相信這完全是由於 Azure 中缺少驅動程序。
有解決方法嗎?
如果有人有興趣,這是我的代碼......
#!/usr/bin/env python3
import adal
from msrestazure.azure_active_directory import AADTokenCredentials
import pyodbc
import struct
resource = "x"
tenant = "x"
authorityHostUrl = "https://login.microsoftonline.com"
clientId = "x"
clientSecret = "x"
authority_uri = authorityHostUrl + '/' + tenant
resource_uri = 'https://database.windows.net/'
context = adal.AuthenticationContext(authority_uri, api_version=None)
mgmt_token = context.acquire_token_with_client_credentials(resource_uri, clientId, clientSecret)
token = mgmt_token["accessToken"]
SQL_COPT_SS_ACCESS_TOKEN = 1256
driver = "{ODBC Driver 17 for SQL Server}"
server = "x.database.windows.net"
database = "x"
connString = "DRIVER=" + driver + ";SERVER=" + server + ";DATABASE=" + database +";Trusted_Connection=False;Encrypt=True;Integrated Security=False;"
tokenb = bytes(token, "UTF-8")
exptoken = b''
for i in tokenb:
exptoken += bytes({i})
exptoken += bytes(1)
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken
conn = pyodbc.connect(connString, attrs_before = {SQL_COPT_SS_ACCESS_TOKEN:tokenstruct})
cursor = conn.cursor()
cursor.execute("SELECT TOP (5) * FROM Reference")
row = cursor.fetchone()
for i in row:
print(i)
對於您的情況,我建議您配置 Hybrid Worker Group,在 HWR 上導入所有需要的模塊並在其上執行您的運行手冊。 有關混合工作器組的更多信息,請參閱此文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.