Error in Azure SQL Server Database connection using Azure Function for python with ActiveDirectoryMSI Authentication(使用带有 ActiveDirectoryMSI 身份验证的 Python 的 Azure 函数连接 Azure SQL Server 数据库时出错)
问题描述
我正在尝试使用 ActiveDirectoryMSI 身份验证从 Azure 函数连接 Azure SQL 数据库.
I am trying to connect the Azure SQL Database from Azure functions for python using ActiveDirectoryMSI Authentication.
请检查以下代码:-
import logging
from . import hy_param
import sys
import pyodbc
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
try:
connection = pyodbc.connect('driver={%s};server=%s;database=%s;Authentication=ActiveDirectoryMSI' % (hy_param.sql_driver, hy_param.server_name, hy_param.database_name))
sql_db = connection.cursor()
logging.info("MSSQL Database Connected")
except Exception as e:
return func.HttpResponse(f"Error in sql database connection : {e}", status_code=400)
sys.exit()
return func.HttpResponse(
"Database Connected",
status_code=200
)
请检查以下错误:-
Error in sql database connection : ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed:self signed certificate] (-1) (SQLDriverConnect)')
有什么方法可以使用 ActiveDirectoryMSI 从 Azure 函数连接 Azure、SQL 数据库?
Is there any way to connect Azure, SQL Database from Azure functions using ActiveDirectoryMSI?
推荐答案
您可以尝试下面的代码使用MSI访问令牌连接到您的Azure SQL(在运行此代码之前,请确保您的功能MSI已启用并且它有权访问您的 Azure SQL):
You can try the code below to use MSI access token to connect to your Azure SQL(Before you run this code , pls make sure your function MSI has been enabled and it has permission to access your Azure SQL ):
import logging
import os
import azure.functions as func
import pyodbc
import requests
import struct
msi_endpoint = os.environ["MSI_ENDPOINT"]
msi_secret = os.environ["MSI_SECRET"]
def main(req: func.HttpRequest) -> func.HttpResponse:
token_auth_uri = f"{msi_endpoint}?resource=https%3A%2F%2Fdatabase.windows.net%2F&api-version=2017-09-01"
head_msi = {'Secret':msi_secret}
resp = requests.get(token_auth_uri, headers=head_msi)
access_token = resp.json()['access_token']
accessToken = bytes(access_token, 'utf-8');
exptoken = b"";
for i in accessToken:
exptoken += bytes({i});
exptoken += bytes(1);
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken;
conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};Server=tcp:andyserver.database.windows.net,1433;Database=database2", attrs_before = { 1256:bytearray(tokenstruct) });
cursor = conn.cursor()
cursor.execute("select @@version")
row = cursor.fetchall()
return func.HttpResponse(str(row))
请使用您赢得的服务器名称和数据库名称编辑连接字符串
Pls edit the connection string with your won server name and db name
这是我这边的测试结果:
This is the test result on my side :
这篇关于使用带有 ActiveDirectoryMSI 身份验证的 Python 的 Azure 函数连接 Azure SQL Server 数据库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用带有 ActiveDirectoryMSI 身份验证的 Python 的 Azure 函数连接 Azure SQL Server 数据库时出错


- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01