Messaging: WIP MailFetcher, MailSession
This commit is contained in:
53
Messaging/Mail/OAuth2.vb
Normal file
53
Messaging/Mail/OAuth2.vb
Normal file
@@ -0,0 +1,53 @@
|
||||
Imports DigitalData.Modules.Base
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports DigitalData.Modules.Messaging.Mail.MailSession
|
||||
Imports Microsoft.Identity.Client
|
||||
|
||||
Public Class OAuth2
|
||||
Inherits BaseClass
|
||||
|
||||
Private ReadOnly TenantId As String
|
||||
Private ReadOnly ClientId As String
|
||||
Private ReadOnly ClientSecret As String
|
||||
|
||||
Public Const O365_SCOPE = "https://outlook.office365.com/.default"
|
||||
Public Const O365_SERVER = "outlook.office365.com"
|
||||
|
||||
Private _AccessToken As String
|
||||
Public ReadOnly Property AccessToken
|
||||
Get
|
||||
Return _AccessToken
|
||||
End Get
|
||||
End Property
|
||||
|
||||
Public Sub New(pLogConfig As LogConfig, pTenantId As String, pClientId As String, pClientSecret As String)
|
||||
MyBase.New(pLogConfig)
|
||||
TenantId = pTenantId
|
||||
ClientId = pClientId
|
||||
ClientSecret = pClientSecret
|
||||
End Sub
|
||||
|
||||
Private Async Function GetAccessToken(pSession As SessionInfo) As Task(Of String)
|
||||
Try
|
||||
' Create the application, which is defined in
|
||||
' Microsoft.Identity.Client
|
||||
Dim oApp = ConfidentialClientApplicationBuilder.
|
||||
Create(pSession.ClientId).
|
||||
WithTenantId(pSession.TenantId).
|
||||
WithClientSecret(pSession.ClientSecret).
|
||||
Build()
|
||||
|
||||
' Request an access token
|
||||
Dim oScopes = New List(Of String) From {O365_SCOPE}
|
||||
Dim oResult = Await oApp.
|
||||
AcquireTokenForClient(oScopes).
|
||||
ExecuteAsync()
|
||||
|
||||
Return oResult.AccessToken
|
||||
Catch ex As Exception
|
||||
Logger.Warn("Could not request access token!")
|
||||
Logger.Error(ex)
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
End Class
|
||||
Reference in New Issue
Block a user