Jump to content

Microsoft 365 Authentication - msal browser launch


Recommended Posts

Here's a quirky one. I would like to have my python script query data from our Microsoft 365 tenant. I have this working fine in my standalone python desktop app, but I'm running in to an issue in a VW script context with loading a browser.

 

The msal library for python deals with secure tokenised login to 365. You call the command 'acquire_token_interactive' and it should launch a browser for you to authenticate with your tenant credentials. Once you've logged in to your company 365 tenant as you, the browser closes and hands the token to your script for your session.

The issue is that it seems when it's time for the browser to launch, Vectorworks crashes. I don't really mind whether it opens the default WebDlg or can be re-directed to an external browser.

Here is my code. I get the AlrtDialog of 'attempt interactive', and then VW quits.

Does anyone have any ideas that might stop Vectorworks crashing and allow the browser to open please?
 

import vs
import logging
from io import BytesIO
import Marionette

lib_OK = False

try:
	import msal
	lib_OK = True

except:
	msal_whl = "https://files.pythonhosted.org/packages/ab/82/8f19334da43b7ef72d995587991a446f140346d76edb96a2c1a2689588e9/msal-1.30.0-py3-none-any.whl"
	bool = Marionette.VerifyOrGetLib("msal",msal_whl)

if lib_OK:
	import requests

class Ctrller_365_Client:
	def __init__(self):

		self.app = msal.PublicClientApplication(
			client_id="redacted for forum",
			authority="redacted for forum",
			token_cache=msal.SerializableTokenCache()
		)
		vs.AlrtDialog('auth app exists')
		self.token = None

	def authenticate(self):
		# Attempt to load token from cache
		accounts = self.app.get_accounts()
		if accounts:
			logging.info("Account(s) found in cache, attempting silent login.")
			self.token = self.app.acquire_token_silent("User.Read", account=accounts[0])
		
		if not self.token:
			vs.AlrtDialog('attempt interactive')
			logging.info("No token in cache or token expired, logging in interactively.")
			self.token = self.app.acquire_token_interactive(scopes="User.Read")

		if "access_token" in self.token:
			logging.info("Authentication successful.")
		else:
			logging.error(f"Authentication failed: {self.token.get('error_description')}")
			raise Exception("Authentication failed.")

 

Edited by spettitt
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...