Login failed for user '<token-identified principal>'
See original GitHub issueEnvironment
- Python: 64-bit
- pyodbc: 4.0.30
- OS: Windows
- DB: SQL Server
- driver: ODBC Driver 17 for SQL Server
Issue
Expected behavior
Establish a connection to SQL Server database.
Observed behavior
Getting an error that I don’t have permission, when I can connect and run basic queries in MSSMS. Only thing I can think of is that there needs to be some sort of permissions thing done on the backend, but it doesn’t make sense that it worked through SQL Server but not pyodbc.
Authentication:

Basic Query:

Code
import pyodbc
server = 'my_server.database.windows.net'
database = 'my_db'
username = '{me@gmail.com}'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username + '; AUTHENTICATION=ActiveDirectoryInteractive')
Error
InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user '<token-identified principal>'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user '<token-identified principal>'. (18456)")
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
AAD Auth Error - Login failed for user '<token-identified ...
An error may appear when trying to login to Azure SQL DB using AAD authentication ... Login failed for user '<token-identified principal>'.
Read more >Login Failed For User Token-Identified Principal In Azure SQL ...
Step 1 - · Step 2 – Set Azure AD admin in SQL Server · Step 3 – Sign in to SQL Database...
Read more >Login failed for user '<token-identified principal>' when web ...
Login failed for user '<token-identified principal>' when web app is in an AAD Group · Deleting and recreating AAD Groups. · Scaling Web...
Read more >"Login failed for user '<token-identified principal>" · Issue #1408
Describe the bug When trying to use service principal authentication, always getting the following error: Login failed for user ' Microsoft.
Read more >Users created by PowerShell gets '<token-identified principal>'
RDM Office 365/Azure AD Users created by PowerShell connected by database user,When the users tries to logging they get: Login failed for user...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just to be sure, you are using the same credentials when you run the Python as you did in the successful DSN test above?
The way the AzureAD authentication to SQL works is that it authenticates to AAD first to get an access token, and then submits that token to SQL during login. What your symptoms show is that in both cases the authentication is successful, but in one case the server rejects the access token.
You could also try to remove the
Databasefrom the connection string in Python, and let Azure connect you to thThanks so much, that did it! Closing issue now.
EDIT: i’m an idiot - was using DATABASE=database.obj.table instead of DATABASE=database… wow*