Connecting to Synapse database using AzureCliCredential token in Spark

PGrover
New Contributor II

I want to connect to my Azure Synapse database using Spark. I can do this in pyodbc no problem but that is not what I want.

Here is how I get my credentials

credential = AzureCliCredential()
databaseToken = credential.get_token('https://database.windows.net/')
 
# get bytes from token obtained
tokenb = bytes(databaseToken[0], "UTF-8")
exptoken = b''
for i in tokenb:
 exptoken += bytes({i})
 exptoken += bytes(1)
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken

I tried this to see if it would work but I am obviously not doing this correctly.

cambio_data_lake = '<synapse database>.sql.azuresynapse.net'
SQL_COPT_SS_ACCESS_TOKEN = '1256'
 
gauges_table = (spark.read
  .format("sqlserver")
  .option("host", cambio_data_lake)
  .option("port", "3342") # optional, can use default port 1433 if omitted
  .option("dbtable", "cambioinspection")
  .option(SQL_COPT_SS_ACCESS_TOKEN,tokenstruct)
  .load()
)

Is there another approach for connecting using a token?

Thanks!