PC SOFT

PROFESSIONAL NEWSGROUPS
WINDEVWEBDEV and WINDEV Mobile

Home → WINDEV 2024 → AuthIdentify - O365Graph
AuthIdentify - O365Graph
Started by Ledoux D, Jul., 29 2021 10:46 AM - No answer
Posted on July, 29 2021 - 10:46 AM
Hello all,

I'm struggling with OAuth2 and Graph.

It's just testing at this point to find a workable situation for our company, so ignore that fact that mailing might be easier in another way.
What I'm trying to achieve at this point its just a mail that needs to be send via the application through graph.

I have registered the application successfully and correctly on the Azure platform also successfully tested via Postman.
First thing I tested was the standard WINDEV teaches with AuthIdentify.
The code beneath works to send the mail.

MyToken is AuthToken
MyTokenParam is OAuth2Parameters

MyTokenParam.ClientID = clientID
MyTokenParam.ClientSecret = clientSecret
MyTokenParam.AuthURL = "https://login.microsoftonline.com/(tennant)/oauth2/v2.0/authorize"
MyTokenParam.TokenURL = "https://login.microsoftonline.com/(tennant)/oauth2/v2.0/token"
MyTokenParam.Scope = "https://graph.microsoft.com/.default"
MyTokenParam.AdditionalParameters = "grant_type=client_credentials"

MyToken = AuthIdentify(MyTokenParam)

IF MyToken <> Null THEN
IF ErrorOccurred THEN
Error(ErrorInfo())
ELSE
req is httpRequest
req.Method = httpPost
req.URL = "https://graph.microsoft.com/v1.0/users/davy.ledoux@mail.be/sendMail"
req.AuthToken = MyToken

req.ContentType = "application/json"
mailJson is JSON <Description="mail">
mailJson.message.body.content = "Deze mail is verzonden met Graph, hopelijk komt hij mooi terecht."
mailJson.message.body.contentType = "Text"
mailJson.message.toRecipients[1].emailAddress.address = "davy.ledoux@mail.be"
mailJson.message.subject = "Graph Test"
req.Content = mailJson

httpResponse is httpResponse = HTTPSend(req)
END
END


But I have an issue with this method.
Each time the AuthIdentify() is called, a browser opens to tell me it successfully got the token and I can "close the browser now".
Since there will be a lot of calls during a day that would mean that I would get spammed with pages opening on a pc/server side.
Is there any way in stopping that webpage from opening?

I also tried the following code.

reqToken is httpRequest
reqToken.Method = httpPost
reqToken.URL = "https://login.microsoftonline.com/{tennant}/oauth2/v2.0/token"
reqToken.Content = "client_id={clientid}&scope=https://graph.microsoft.com/.default&client_secret={secret}&grant_type=client_credentials"
HTTPres is httpResponse = HTTPSend(reqToken)
vVar is Variant = JSONToVariant(HTTPres.Content)


This successfully gets the Bearer token but then I seem to be unable to use it to execute the request to mail with that token.
I tried making a AuthToken with the HTTPres response.
MyTokenParam is OAuth2Parameters

MyTokenParam.ClientID = clientID
MyTokenParam.ClientSecret = clientSecret
MyTokenParam.AuthURL = "https://login.microsoftonline.com/(tennant)/oauth2/v2.0/authorize"
MyTokenParam.TokenURL = "https://login.microsoftonline.com/(tennant)/oauth2/v2.0/token"
MyTokenParam.Scope = "https://graph.microsoft.com/.default"
//MyTokenParam.AdditionalParameters = "grant_type=client_credentials"

MyToken is AuthToken(MyTokenParam,VariantToJSON(vVar))

But that doesn't seem to be working since no mail is send.

I also tried to not use an AuthToken and tried a normal http request with header option but that also doesn't seem to be working.
req is httpRequest
req.Method = httpPost
req.URL = "https://graph.microsoft.com/v1.0/users/davy.ledoux@mail.be/sendMail"
req.Header["Authorization"] = "Bearer " + vVar.access_token // Authentication token

//CreateMail
req.ContentType = "application/json"
mailJson is JSON <Description="mail">
mailJson.message.body.content = "Deze mail is verzonden met Graph, hopelijk komt hij mooi terecht."
mailJson.message.body.contentType = "Text"
mailJson.message.toRecipients[1].emailAddress.address = "davy.ledoux@mail.be"
mailJson.message.subject = "Graph Test"
req.Content = mailJson

httpResponse is httpResponse = HTTPSend(req)


Am I doing something wrong here or is what I'm trying to do impossible without AuthIdentify() and hence opening a browser page?

Thx for the help.

Kind Regards,
Davy.