Python script failing to connect to Azure SQL Database

I get to the https://login.microsoftonline.com/ page and I get a “Authentication complete. You can close this window.” message. I’m running the following script. I can’t figure out if there’s something wrong with the script. On the other side, my IP is already added. My user has admin.
Anyway, find any problems?

import pyodbc
import pandas as pd
from azure.identity import InteractiveBrowserCredential

#Azure SQL Database connection parameters
server = 'conosur.database.windows.net'
database = 'legacy (conosur/legacy)'
driver = '{ODBC Driver 17 for SQL Server}'

#Set up the Azure AD credential
credential = InteractiveBrowserCredential()

#Obtain an Azure AD token
token = credential.get_token('https://database.windows.net/.default')

#Create the connection string with the token
connection_string = (
f'DRIVER={driver};'
f'SERVER={server};'
f'DATABASE={database};'
f'Authentication=ActiveDirectoryAccessToken;'  # Correct Authentication parameter
f'ACCESS_TOKEN={token.token}'  # Correctly set the access token
)

try:
connection = pyodbc.connect(connection_string)
print("Connection established successfully")
except pyodbc.Error as ex:
sqlstate = ex.args[0]
print(f"Error connecting to SQL Server: {sqlstate}")
print(f"Detailed error: {ex}")
exit()

#Define query
query = 'SELECT * FROM your_table_name'
try:
df = pd.read_sql(query, connection)
print("DataFrame created successfully")
except Exception as e:
print(f"Error executing query: {e}")
finally:
connection.close()  # Ensure the connection is closed

#Display the DataFrame
print(df.head())

Error message:
Error connecting to SQL Server: 08001 Detailed error: ('08001', "[08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid value specified for connection string attribute 'Authentication' (0) (SQLDriverConnect)")

I’m hoping to log in with my Entra ID in the prompted window. But after doing it, I get the error message above.

3

Detailed error: (‘08001’, “[08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid value specified for connection string attribute ‘Authentication’ (0) (SQLDriverConnect)”)

You have provided ActiveDirectoryAccessToken for Authentication parameter in connection string, it is invalid that may be the reason forgetting above error. To fix this, you should use the ActiveDirectoryInteractive authentication method, which is designed for scenarios where the user must manually authenticate (e.g., through a browser popup). You can use below code to connect azure SQL database:

import pyodbc

server = '<serverName>.database.windows.net'
database = '<dbName>'
driver = '{ODBC Driver 17 for SQL Server}'
connection_string = (
f'DRIVER={driver};'
f'SERVER={server};'
f'DATABASE={database};'
f'Authentication=ActiveDirectoryInteractive;' 
)
try:
  connection = pyodbc.connect(connection_string)
  print("Connection established successfully")
except pyodbc.Error as ex:
  print(f"Detailed error: {ex}")

It will give a pop-up window for authentication, after successful authentication you will be able connect SQL database successfully as shown below:

Recognized by Microsoft Azure Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật