Author Topic: How to Access Microsoft Graph via Visual Lisp?  (Read 263 times)

0 Members and 1 Guest are viewing this topic.

CodeDing

  • Newt
  • Posts: 55
How to Access Microsoft Graph via Visual Lisp?
« on: May 13, 2024, 07:06:09 PM »
Hey all,

So, I know I can make a simple GET/POST request via Visual Lisp, something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun web-get (url / http_object response)
  2.   (setq http_object (vlax-create-object "WinHTTP.WinHTTPRequest.5.1"))
  3.   (vlax-invoke-method http_object 'open "GET" url :vlax-false)
  4.   (vlax-invoke-method http_object 'send)
  5.   (setq response (vlax-get-property http_object "ResponseText"))
  6.   (if http_object (vlax-release-object http_object))
  7.   response
  8. )
  9.  
  10. Command: (web-get "https://jsonplaceholder.typicode.com/todos/1")
  11. "{\n  \"userId\": 1,\n  \"id\": 1,\n  \"title\": \"delectus aut autem\",\n  \"completed\": false\n}"
  12.  

And I can use Microsoft's Graph API to make simple requests about my profile:
https://developer.microsoft.com/en-us/graph/graph-explorer
So, when I run the test "https://graph.microsoft.com/v1.0/me", I can get my profile info.

But the only way that request is fulfilled is because when using the Graph Explorer, the "Access Token" for myself is automatically sent.

I DO KNOW that I can copy that Access Token, then hard-code it into my (web-get ...) function so that I can get my profile...

What I would like to know is HOW can I make a request to GET my Access Token, to subsequently make a second request to get my profile info?

Workflow would look something like this:
- Command initiated to get Profile Info
- Program makes request for Access Token of current logged-in user
- Access Token returned
- Program makes request for profile of same user
- Profile info returned

I should probably note that it would be on a Dell Machine, Windows OS, & I am a user within a company, so I am logged-in as a company user with a default Microsoft Office account 'tied' to my logged in account (idk how it all ties together exactly).

Can retrieving the Access Token via a GET/POST request be accomplished in Visual Lisp?

Best
~DD
Senior CAD Tech & AI Specialist
Need AutoLisp help?
Try my custom GPT 'AutoLISP Ace'

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8766
  • AKA Daniel
Re: How to Access Microsoft Graph via Visual Lisp?
« Reply #1 on: May 13, 2024, 07:47:57 PM »
you might have a look through the python SDK for hints
https://github.com/microsoftgraph/msgraph-sdk-python

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8766
  • AKA Daniel
Re: How to Access Microsoft Graph via Visual Lisp?
« Reply #2 on: May 13, 2024, 07:56:36 PM »
What is this, spyware for companies? 

BIGAL

  • Swamp Rat
  • Posts: 1425
  • 40 + years of using Autocad
Re: How to Access Microsoft Graph via Visual Lisp?
« Reply #3 on: May 13, 2024, 08:54:37 PM »
Do you just want "Username" ? That is a big Hint. getenv.
A man who never made a mistake never made anything

CodeDing

  • Newt
  • Posts: 55
Re: How to Access Microsoft Graph via Visual Lisp?
« Reply #4 on: May 14, 2024, 10:26:45 AM »
What is this, spyware for companies? 

:shifty: I guess I hadn't thought of it like that  :lol:
But the Python docs were pretty helpful for seeing some of the setup involved!

Do you just want "Username" ? That is a big Hint. getenv.

I'm trying to retrieve department / manager of the user to help determine which programs should load for them! I could hard-code it, but then it needs updated anytime someone new comes, if people leave, etc. Would be nice to keep it dynamic.


..... So if I'm understanding this process a little better, It seems that I am desiring delegated credentials for the user, but perhaps I still need somekind of Client key? So I would have to register an app for the company on Azure or something? I'm not totally familiar with all this web business like web apps and what not. But I'd like to get my feet wet with it where I can
Senior CAD Tech & AI Specialist
Need AutoLisp help?
Try my custom GPT 'AutoLISP Ace'

BIGAL

  • Swamp Rat
  • Posts: 1425
  • 40 + years of using Autocad
Re: How to Access Microsoft Graph via Visual Lisp?
« Reply #5 on: May 14, 2024, 08:08:07 PM »
I had plot routines that did as I suggested already, looked at user name and would send output to correct printer near them, we had a multi story building. Again (getenv "Username")

Yes if people leave have to change the list of names.

Note username is returned for current logged in user on a pc.
A man who never made a mistake never made anything

MickD

  • King Gator
  • Posts: 3644
  • (x-in)->[process]->(y-out) ... simples!
Re: How to Access Microsoft Graph via Visual Lisp?
« Reply #6 on: May 14, 2024, 10:38:55 PM »
It seems to be able to access the api you need to register your 'app' then you can have at it.
https://learn.microsoft.com/en-us/graph/auth-register-app-v2

Do you need to use MS Graph? If not you could build your own simple web server on the company intranet or host it in the cloud. Something like Go with SQLite db would be easy enough to setup, you could even register it and then use it with the MS Graph api to get extra data if needed.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8766
  • AKA Daniel
Re: How to Access Microsoft Graph via Visual Lisp?
« Reply #7 on: Today at 02:32:30 AM »
:shifty: I guess I hadn't thought of it like that  :lol:

Well, I watched a couple of videos and one of them mentioned, and I’m paraphrasing,
companies can access employee emails via the REST API to scan for keyword words as a search tool.

Of course, the conspiracist in me immediately thought, scan employee emails and feed them to an AI to build profiles  :evil:
 

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8766
  • AKA Daniel
Re: How to Access Microsoft Graph via Visual Lisp?
« Reply #8 on: Today at 02:37:57 AM »
Do you need to use MS Graph?

With MS Graph you would have everything. From device info, who they have lunch schedules with, and Sally’s next fingernail appointment

MickD

  • King Gator
  • Posts: 3644
  • (x-in)->[process]->(y-out) ... simples!
Re: How to Access Microsoft Graph via Visual Lisp?
« Reply #9 on: Today at 03:51:56 AM »
Do you need to use MS Graph?

With MS Graph you would have everything. From device info, who they have lunch schedules with, and Sally’s next fingernail appointment

No doubt!  :-D
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

CodeDing

  • Newt
  • Posts: 55
Re: How to Access Microsoft Graph via Visual Lisp?
« Reply #10 on: Today at 10:55:45 AM »
Do you need to use MS Graph? If not you could build your own simple web server on the company intranet or host it in the cloud. Something like Go with SQLite db would be easy enough to setup, you could even register it and then use it with the MS Graph api to get extra data if needed.
^^^ I believe this would be the same concept as hard-coding it into our acad.lsp file. It would still need updated by me, but I'd rather have it come from Graph since IT has to update these profiles already anyways  ;-)

Of course, the conspiracist in me immediately thought, scan employee emails and feed them to an AI to build profiles  :evil:
:laugh: That would be bonkers. Plus, that's what Copilot M365 is for yeah?  :whistling:
Senior CAD Tech & AI Specialist
Need AutoLisp help?
Try my custom GPT 'AutoLISP Ace'

MickD

  • King Gator
  • Posts: 3644
  • (x-in)->[process]->(y-out) ... simples!
Re: How to Access Microsoft Graph via Visual Lisp?
« Reply #11 on: Today at 04:51:41 PM »
Quote
I believe this would be the same concept as hard-coding it into our acad.lsp file

I'm not sure what you mean by this, your lisp just queries your api to get the data for the user (like your json return sample) and do what you need. If you mean you are using/instructing Graph itself to install MS app's (not lisp/arx app's in cad) then I understand.

As I mentioned, you can also use the web app as a go between to Graph (IT can still handle profiles), then you only need to register one application with Graph, only users on company machines will have access to the web app via your lisp so security risks are minimal.
Your problem at the moment is you would need to use some authentication to gain access and you don't want or need each user to have direct access, your lisp with web app is the gate keeper.

I hope that makes sense :)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien