Skip to content

Instantly share code, notes, and snippets.

@ArthurSteijn
Created February 5, 2022 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArthurSteijn/453008cc2fe3790cb28af3a0ef40076f to your computer and use it in GitHub Desktop.
Save ArthurSteijn/453008cc2fe3790cb28af3a0ef40076f to your computer and use it in GitHub Desktop.
import pyodbc
import struct
def use_token(server_name, access_token):
connection_string = 'Driver={ODBC Driver 17 for SQL Server};server='+server_name+';database=master;'
SQL_COPT_SS_ACCESS_TOKEN = 1256
exptoken = b''
for i in bytes(access_token, "UTF-8"):
exptoken += bytes({i})
exptoken += bytes(1)
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken
cnxn = pyodbc.connect(connection_string, attrs_before = { SQL_COPT_SS_ACCESS_TOKEN:tokenstruct })
cursor = cnxn.cursor()
return cursor, cnxn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment