Reputation: 103
According to this page.
AAD Token-based authentication to access Azure SQL DB is supported only if client is under windows environment.
Could MacOS and Linux support AAD Token-based authentication to access Azure SQL DB?
https://github.com/mkleehammer/pyodbc/issues/228
token = context.acquire_token_with_client_credentials(
database_url,
azure_client_id,
azure_client_secret
)
print(token)
tokenb = bytes(token["accessToken"], "UTF-8")
exptoken = b''
for i in tokenb:
exptoken += bytes({i})
exptoken += bytes(1)
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken
tokenstruct
SQL_COPT_SS_ACCESS_TOKEN = 1256
CONNSTRING = "DRIVER={};SERVER={};DATABASE={}".format("ODBC Driver 17 for SQL Server", prod_server, prod_db)
db_connector = pyodbc.connect(CONNSTRING, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: tokenstruct})
This is the code I run under MacOS and it is python.
I keep getting this issue
pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user ''. (18456) (SQLDriverConnect)")
Anyone has a idea?
Upvotes: 1
Views: 3934
Reputation: 5549
It seems that you have not added your application service principal to your Azure SQL database .
What you need to do is to:
CREATE USER [Azure_AD_principal_name] FROM EXTERNAL PROVIDER;
EXEC sp_addrolemember 'db_owner', 'Azure_AD_principal_name';
Here, the Azure_AD_principal_name
should be the application's name.
Upvotes: 2