Microsoft Graph�ɓ�������Ă���Outlook���[��REST API���g�p���āA�d�q���[���𑗐M������@���������B
�@Microsoft�A�J�E���g������Aoutlook.com�Ń��[���̑���M���ł���B����ɂ�Outlook�A�v����Outlook��Web�T�C�g�𗘗p���鑼�ɁARESTful Web API�iREST API�j�ł���uMicrosoft Graph�v���g�����@������BREST API�́A�Ǝ��̃A�v����Web�T�C�g���痘�p�ł���B�G���h���[�U�[��Microsoft�A�J�E���g�������Ă��邱�Ƃ��O��̃f�X�N�g�b�v�A�v����A�g�D��Microsoft�A�J�E���g���g���ă��[���𑗎�M����V�X�e���ȂǂŁA����REST�T�[�r�X�͖��ɗ����낤�B�{�e�ł́AMicrosoft Graph API���g���ă��[���𑗐M������@���������B
�@�Ȃ��AOutlook��Web�T�C�g��outlook.live.com�ł���AMicrosoft Graph API������Ă���T�C�g��graph.microsoft.com�ł���B���ۂɃ��[�����M�Ɏg����T�[�o�̃h���C����outlook.com�ł���̂ŁA�{�e�ł́uoutlook.com�Ń��[���̑���M�v�ƕ\�����Ă���B
�@����̃g�s�b�N�������ɒm�肽���Ƃ������͈ȉ��̃����N�����p���Ăق����BGraph API�̊�b�m�����������̕��́A�O�������āu�e�L�X�g���[����g�ݗ��Ă�ɂ́H�v�ɐi��ł������������B
�@�Ȃ��A�{�e�Ɍf�ڂ����T���v���R�[�h�����̂܂����ɂ�Visual Studio 2017�ȍ~���K�v�ł���B�T���v���R�[�h�̓R���\�[���A�v���̈ꕔ�ł���A�R�[�h�̖`���Ɉȉ��̐錾���K�v�ƂȂ�B�܂��AC#�̃T���v���R�[�h�ɂ́AC# 7.1�ȍ~���K�v�ł���B
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using static System.Console;
Imports System.Console
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports Microsoft.Graph
Imports Microsoft.Identity.Client
�@Microsoft Graph API�Ƃ́AMicrosoft�̃N���E�h�T�[�r�X�𗘗p���邽�߂̋���API���ƍl����ƕ�����₷���B����̃T�[�r�X�𗘗p���邽�߂�API���p�ӂ���Ă���B���̊T���́A�uMicrosoft Graph �̊T�v�v�������������������i���̉摜�j�B���Ȃ݂ɁABuild 2015�Ŕ��\���ꂽ�Ƃ��ɂ́A�uOffice Graph API�v�^�uOffice 365 Unified API�v�ȂǂƌĂ�Ă����i�u���W�FBuild 2015�F�S�Ă̊J���҂��������Ă����ׂ��}�C�N���\�t�g�̍ŐV�Z�p���� �i2/5�j�v�̃y�[�W�̉����ŏЉ�Ă���j�B
�@Microsoft Graph�Œ���Ă��邳�܂��܂ȃT�[�r�X�̒�����AOutlook�̋@�\�́A����ɂ��̈ꕔ�ł��郁�[�����M��{�e�ł͗��p���Ă݂悤�Ƃ����킯���B
�@Microsoft Graph��REST API�ł��邩��A�A�v���Œ���HTTP���N�G�X�g��g�ݗ��Ă�API�𗘗p���邱�Ƃ��ł���B�Ⴆ���[�����M�Ȃ�u���[���𑗐M���� - �h�L�������g - Microsoft Graph�v�������������������i���̉摜�j�B
�@���̂悤��HTTP���N�G�X�g��g�ݗ��āA���M���A��������ʂ���͂���R�[�h�������͖̂ʓ|�Ȃ��̂��B�N���C�A���g�A�v������g�����߂̃��C�u�����uMicrosoft Graph .NET Client Library�v�i�ȍ~�A�uGraph Client Library�v�j��Microsoft���烊���[�X����Ă���̂ŁA������g�����B.NET Standard 1.1�ɑΉ����Ă���̂ŁA.NET Framework�����łȂ��AUWP�A�v����Xamarin�Ȃǂ�������p�ł���B
�@NuGet����Visual Studio�̃v���W�F�N�g�ɓ�������BGraph Client Library�̑��ɁA���[�U�[�F�̂��߂̃��C�u�������K�v���B
�@�܂�Graph Client Library�ł��邪�ANuGet�ŁuMicrosoft.Graph�v����������Ό�����i���̉摜�j�B
�@Graph API�𗘗p���邽�߂̃��[�U�[�F�ɂ͊���̕��@������i��q�j�B�����ł́AMicrosoft Authentication Library�iMSAL�j���g�����B�܂��v���r���[�̂܂܂ɂȂ��Ă��邪�A���łɏ\���Ɉ��肵�Ă���BVisual Studio��NuGet�p�b�P�[�W�Ǘ���ʂŁm�v�������[�X���܂߂�n�Ƀ`�F�b�N�����Ă���uMicrosoft.Identity.Client�v����������i���̉摜�j�B
�@�]���̃f�X�N�g�b�v�A�v���Ȃǂ̏ꍇ�́A���炩����Graph API�𗘗p����A�v�����A�v���P�[�V�����o�^�|�[�^���iApplication Registration Portal�j�ɓo�^���A�A�v���P�[�V����ID�i�N���C�A���gID�j���擾����iUWP�A�v���̏ꍇ�́A�X�g�A�ɓo�^���邱�ƂŎ����I�ɓo�^�����j�B�o�^�ɂ́AMicrosoft�A�J�E���g���K�v���B
�@�A�v���P�[�V�����o�^�|�[�^���ɃT�C���C��������A�u�W���^�A�v���P�[�V�����v�iconverged applications���u�����A�v���v�j��lj����āA�A�v���̖��O�Ȃǂ�o�^����B�o�^�菇�̊T�v�����̉摜�Ɏ����̂ŁA�Q�l�ɂ��Ă��炢�����i����UI�͕p�ɂɕύX�����̂ŁA�قȂ��Ă���\��������j�B����̏ꍇ�A�o�^���e�ŏd�v�Ȃ̂́A�A�v���̖��O�ƃA�N�Z�X�����B�A�v���̖��O�́A���̂܂܃��[�U�[�F��UI�ɕ\�������B�A�N�Z�X�����������Ȃ��ƁA�A�v�������API�Ăяo�������₳���B�܂��A�A�v���P�[�V����ID�́A��قǃR�[�f�B���O����Ƃ��̃N���C�A���gID�Ƃ��Ďg���̂ŁA�������߂Ă����B
�A�v����o�^����菇�̊T�v
�A�N�Z�X����2��ނ��邪�A�G���h���[�U�[��Microsoft�A�J�E���g�ŃT�C���C�����Ă�����āi���G���h���[�U�[�Ɍ������ϔC���Ă�����āj�A�v�����A�N�Z�X����ꍇ�́u�ϔC���ꂽ�A�N�Z�X���v��I�ԁB
�@�قƂ�ǂ�Graph API�́A�Ăяo���Ƃ��ɃA�N�Z�X�g�[�N�����K�v�ł���BAzure AD�ŔF����ƁA�A�N�Z�X�g�[�N������B�ȉ��Ɏ����悤�ɁA���̔F�ɂ�2�ʂ�̕��@������B
�@�G���h���[�U�[���F���s���ꍇ�A���̃A�J�E���g����ʓI��Microsoft�A�J�E���g���A����Ƃ��A�E��܂��͊w�Z�A�J�E���g�iAzure AD�̃A�J�E���g�j�Ȃ̂��ɂ���āA�ȉ��̂悤�ɗ��p�ł���F�ؕ��@���قȂ�B
�@�����Microsoft�A�J�E���g���g�������̂ŁAAzure AD v2.0��I�ԁB����REST API�����b�v�������C�u�������A�u���C�u����������ɂ́H�v�ŃC���X�g�[������Microsoft Authentication Library�iMSAL�j�ł���B
�@�b�������Ȃ������A�����܂ł�Graph API�̊�b�m���ł���B�悤�₭�R�[�f�B���O�Ɏ��|���鏀�����ł����B
�@���āAMSAL���g���ă��[�U�[�F����R���\�[���A�v���͎��̃R�[�h�̂悤�ɂȂ�B�ꕔ�A�u*�v�����ŕ������ɂ��Ă��邪�A���ۂɂ͓K�ȕ������ݒ肵�Ăق����i�ȉ������j�B
class Program
{
// ClientID�ɂ́A�A�v����o�^�����Ƃ��̃A�v���P�[�V����ID��ݒ肷��
const string ClientID = "********-****-****-****-************";
private static readonly string RedirectUri = $"msal{ClientID}://auth";
// ���[�U�[�F�؊W�̃v���p�e�B�i��قǁA�ʂ̃N���X������A�N�Z�X����j
public static PublicClientApplication PCA { get; }
= new PublicClientApplication(ClientID) { RedirectUri = RedirectUri, };
public static IReadOnlyList<string> Scopes { get; }
= new List<string> { "User.Read", "Mail.Send", }.AsReadOnly();
// �uasync Task Main�v�Ə�����̂�C# 7.1�ȍ~
static async Task Main(string[] args)
{
AuthenticationResult ar = null; // �F�،��ʁi�A�N�Z�X�g�[�N���Ȃǂ�����j
try
{
// �܂��T�C�����g�T�C���C��������
try
{
IAccount signedInUser
= (await Program.PCA.GetAccountsAsync()).FirstOrDefault();
if(signedInUser != null)
ar = await PCA.AcquireTokenSilentAsync(Scopes, signedInUser);
// �T�C�����g�T�C���C���Ɏ��s�����MsalUiRequiredException���o��
}
catch { }
// �T�C�����g�T�C���C�����ł��Ȃ�������A�T�C���C����UI���o��
if (ar == null)
ar = await PCA.AcquireTokenAsync(Scopes);
WriteLine("�T�C���C�����܂����B");
WriteLine($"AccessToken={ar.AccessToken}");
WriteLine($"Username={ar.Account.Username}");
WriteLine($"ExpiresOn={ar.ExpiresOn.LocalDateTime.ToString("HH:mm:ss")}");
WriteLine($"Scopes={string.Join(", ", ar.Scopes)}");
// �o�͗�F
// �T�C���C�����܂����B
// AccessToken=EwBwA8l6BAAURSN�c�c�����c�cRd2Ag==
// Username=biac@***.com
// ExpiresOn=23:02:13
// Scopes=mail.send, openid, profile, user.read
}
catch(MsalException ex)
{
// �F�����Ƀ_�C�A���O�����Ɨ�O���o��
WriteLine($"{ex.GetType().Name}:{ex.ErrorCode}");
}
if (ar == null)
{
// �T�C���C���ł��Ȃ������̂ŃA�v���I��
#if DEBUG
ReadKey();
#endif
return;
}
// ������Graph API���g�����������s��
// �T�C���A�E�g����
await PCA.RemoveAsync(ar.Account);
WriteLine("�T�C���A�E�g���܂���");
#if DEBUG
ReadKey();
#endif
}
}
Module Module1
' ClientID�ɂ́A�A�v����o�^�����Ƃ��̃A�v���P�[�V����ID��ݒ肷��
Const ClientID As String = "********-****-****-****-************"
Private ReadOnly RedirectUri As String = $"msal{ClientID}://auth"
' ���[�U�[�F�؊W�̃v���p�e�B
Public ReadOnly Property PCA As PublicClientApplication _
= New PublicClientApplication(ClientID) With {.RedirectUri = RedirectUri}
Public ReadOnly Property Scopes As IReadOnlyList(Of String) _
= New List(Of String) From {"User.Read", "Mail.Send"}.AsReadOnly()
Sub Main()
Task.Run(Async Function() As Task
Await SendMailAsync()
End Function).Wait()
#If DEBUG Then
ReadKey()
#End If
End Sub
Async Function SendMailAsync() As Task
Dim ar As AuthenticationResult = Nothing ' �F�،��ʁi�A�N�Z�X�g�[�N���Ȃǂ�����j
Try
' �܂��T�C�����g�T�C���C��������
Try
Dim signedInUser As IAccount _
= (Await PCA.GetAccountsAsync()).FirstOrDefault()
If (signedInUser IsNot Nothing) Then
ar = Await PCA.AcquireTokenSilentAsync(Scopes, signedInUser)
' �T�C�����g�T�C���C���Ɏ��s�����MsalUiRequiredException���o��
End If
Catch
End Try
' �T�C�����g�T�C���C�����ł��Ȃ�������A�T�C���C����UI���o��
If (ar Is Nothing) Then
ar = Await PCA.AcquireTokenAsync(Scopes)
End If
WriteLine("�T�C���C�����܂����B")
WriteLine($"AccessToken={ar.AccessToken}")
WriteLine($"Username={ar.Account.Username}")
WriteLine($"ExpiresOn={ar.ExpiresOn.LocalDateTime.ToString("HH:mm:ss")}")
WriteLine($"Scopes={String.Join(", ", ar.Scopes)}")
' �o�͗�F
' �T�C���C�����܂����B
' AccessToken=EwBwA8l6BAAURSN�c�c�����c�cRd2Ag==
' Username=biac@***.com
' ExpiresOn=23:02:13
' Scopes=mail.send, openid, profile, user.read
Catch ex As MsalException
' �F�����Ƀ_�C�A���O�����Ɨ�O���o��
WriteLine($"{ex.GetType().Name}:{ex.ErrorCode}")
Catch ex As Exception
WriteLine(ex.ToString())
End Try
If (ar Is Nothing) Then
' �T�C���C���ł��Ȃ������̂ŃA�v���I��
Return
End If
' ������Graph API���g�����������s��
' �T�C���A�E�g����
Await PCA.RemoveAsync(ar.Account)
WriteLine("�T�C���A�E�g���܂���")
End Function
End Module
�@��̃R�[�h�����s����ƁA���̉摜�̂悤�ȔF�_�C�A���O���\�������B
MSAL�̔F�_�C�A���O
���̃_�C�A���O��MSAL�̃��C�u�������o���Ă���B�����œ��͂����p�X���[�h�́A�A�v���ɂ͓n����Ȃ��B���C�u�������g�킸��Azure AD�̔F����ꍇ�ł��A���l��Web�y�[�W���\������A��͂�p�X���[�h���A�v���ɓn����邱�Ƃ͂Ȃ��B
�@���[���̃��b�Z�[�W�́AMessage�N���X�iMicrosoft.Graph���O��ԁj�ŕ\�����B���Ȃ݂ɁAMessage�N���X��OutlookItem�N���X�iMicrosoft.Graph���O��ԁj���p�����Ă���B
�@�e�L�X�g���[���̃��b�Z�[�W��g�ݗ��Ă�ɂ́AMessage�N���X�̃C���X�^���X�����A���̃v���p�e�B��ݒ肷��悢�i���̃R�[�h�j�B�����R�[�h���w�肷����@�͂Ȃ����AJIS�R�[�h�ŃG���R�[�f�B���O����đ��M�����i���炭�AWindows�̌���ݒ������Graph API���Ăяo���Ƃ���Accept-Language�𑗐M���A����ɂ���Ĕ��肵�Ă�����̂Ǝv����j�B
// ���[���쐬
var email = new Message
{
ToRecipients = new Recipient[] {
new Recipient {
EmailAddress = new EmailAddress {
Name = "outlook.com���烁�[����M",
Address = "***@***.jp"
}
},
// Recipient�I�u�W�F�N�g�͕����w��\�iCC�^BCC���j
},
// CcRecipients = �c�c�ȗ��c�c
// BccRecipients = �c�c�ȗ��c�c
Subject = "Graph API�Ń��[���𑗐M����e�X�g",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "Microsoft GRAPH���g�������[�����M�̃e�X�g�B",
},
};
' ���[���쐬
Dim email = New Message With
{
.ToRecipients = {
New Recipient With {
.EmailAddress = New EmailAddress With {
.Name = "outlook.com���烁�[����M",
.Address = "***@***.jp"
}
}
},
.Subject = "Graph API�Ń��[���𑗐M����e�X�g",
.Body = New ItemBody With
{
.ContentType = BodyType.Text,
.Content = "Microsoft GRAPH ���g�������[�����M�̃e�X�g�B"
}
}
' CC�iCcRecipients�v���p�e�B�j�^BCC�iBccRecipients�v���p�e�B�j���w���
' Recipient�I�u�W�F�N�g�͕����w��\�iTO�^CC�^BCC�j
�@�Ȃ��AMessage�N���X�ɂ�From�v���p�e�B�����邪�A�ʏ�͐ݒ肵�Ȃ��B�T�C���C�������A�J�E���g�̃��[���A�h���X���g����B�T�C���C�������A�J�E���g�ɕ����̃��[���A�h���X���o�^����Ă���ꍇ�ɂ́A�T�C���C���Ɏg�������̂Ƃ͈قȂ郁�[���A�h���X��From�v���p�e�B�ɐݒ肷�邱�Ƃ͉\���B�������A���̏ꍇ��Name�v���p�e�B�͖�������āA�o�^����Ă��閼�O���g����B
�@�������A�T�C���C�������A�J�E���g�Ɍ��ѕt�����Ă��Ȃ����[���A�h���X��From�v���p�e�B�ɐݒ肵�Ă��܂����ꍇ�́A���[�����M���G���[�ɂȂ���肩�A���̃A�J�E���g����������Ă��܂����Ƃ�����̂Œ��ӂ��K�v���i���̉摜�j�B�v����ɁAFROM�̍��̂͋����Ȃ��Ƃ����킯�ł���B
�@MSAL���C�u������Graph API���g���ɂ́AGraphServiceClient�N���X�iMicrosoft.Graph���O��ԁj�𗘗p����B
�@Graph API�Ƀ��N�G�X�g�𑗐M����Ƃ��ɃA�N�Z�X�g�[�N����Y�t����K�v������̂����AMSAL�ł͂��̏�����IAuthenticationProvider�C���^�t�F�[�X�iMicrosoft.Graph���O��ԁj�ōs���悤�ɐv����Ă���B���̎������ɍ���Ă������i���̃R�[�h�j�B
public class MsalAuthenticationProvider : IAuthenticationProvider
{
public async Task AuthenticateRequestAsync(HttpRequestMessage request)
{
// �m���ɗL���ȃA�N�Z�X�g�[�N���邽�߂ɁA�T�C�����g�T�C���C�����s��
IAccount signedInUser
= (await Program.PCA.GetAccountsAsync()).FirstOrDefault();
AuthenticationResult ar
= await Program.PCA.AcquireTokenSilentAsync(Program.Scopes, signedInUser);
// �A�N�Z�X�g�[�N����request�ɃZ�b�g����
request.Headers.Authorization
= new AuthenticationHeaderValue("Bearer", ar.AccessToken);
}
}
Public Class MsalAuthenticationProvider
Implements IAuthenticationProvider
Public Async Function AuthenticateRequestAsync(request As HttpRequestMessage) As Task _
Implements IAuthenticationProvider.AuthenticateRequestAsync
' �m���ɗL���ȃA�N�Z�X�g�[�N���邽�߂ɁA�T�C�����g�T�C���C�����s��
Dim signedInUser As IAccount = (Await PCA.GetAccountsAsync()).FirstOrDefault()
Dim ar As AuthenticationResult _
= Await PCA.AcquireTokenSilentAsync(Scopes, signedInUser)
' �A�N�Z�X�g�[�N����request�ɃZ�b�g����
request.Headers.Authorization _
= New AuthenticationHeaderValue("Bearer", ar.AccessToken)
End Function
End Class
�@���ۂɃ��[���𑗐M����R�[�h�́A���̂悤�ɃV���v���Ȃ��̂��B
// ���[�����M
try
{
GraphServiceClient gsc
= new GraphServiceClient(new MsalAuthenticationProvider());
await gsc.Me.SendMail(email).Request().PostAsync();
WriteLine("���[�����M����");
}
catch (Exception ex)
{
WriteLine("���[�����M���s");
WriteLine(ex.ToString());
}
' ���[�����M
Try
Dim gsc As GraphServiceClient _
= New GraphServiceClient(New MsalAuthenticationProvider())
Await gsc.Me.SendMail(email).Request().PostAsync()
WriteLine("���[�����M����")
Catch ex As Exception
WriteLine("���[�����M���s")
WriteLine(ex.ToString())
End Try
�@���M���郁�[���Ƀt�@�C����Y�t����ɂ́AMessage�I�u�W�F�N�g��Attachments�v���p�e�B��ݒ肷��悢�i���̃R�[�h�j�B�}���`�p�[�g���[���̎d�g�݂��ӎ����邱�ƂȂ��A�ȒP�ɃR�[�f�B���O�ł���B
email.Attachments = new MessageAttachmentsCollectionPage();
string path = @"C:\Windows\Web\Wallpaper\Theme2\img10.jpg"; // �Y�t�������t�@�C��
email.Attachments.Add(new FileAttachment
{
ODataType = "#microsoft.graph.fileAttachment",
ContentBytes = System.IO.File.ReadAllBytes(path),
ContentType = "image/jpeg",
Name = System.IO.Path.GetFileName(path),
});
email.Attachments = New MessageAttachmentsCollectionPage()
Dim path As String = "C:\Windows\Web\Wallpaper\Theme2\img10.jpg" ' �Y�t�������t�@�C��
email.Attachments.Add(New FileAttachment With
{
.ODataType = "#microsoft.graph.fileAttachment",
.ContentBytes = System.IO.File.ReadAllBytes(path),
.ContentType = "image/jpeg",
.Name = System.IO.Path.GetFileName(path)
})
�@�G���h���[�U�[��Microsoft�A�J�E���g���g���āAoutlook.com���烁�[���𑗐M�ł���BMicrosoft Graph API���g����悤�ɂ���܂ł̏�����������Ɩ��ł��邪�A���[��������đ��M����R�[�h�͊ȒP�ł���B
���p�\�o�[�W�����F.NET Framework 4.5�ȍ~
�J�e�S���F�I�[�v���\�[�X���C�u�����@�����ΏہFWindows�t�H�[��
�J�e�S���F�I�[�v���\�[�X���C�u�����@�����ΏہFWPF
�J�e�S���F�I�[�v���\�[�X���C�u�����@�����ΏہFXamarin.Forms
�J�e�S���F�N���X���C�u�����@�����ΏہF�d�q���[��
�g�p���C�u�����FMessage�N���X�iMicrosoft.Graph���O��ԁj
�g�p���C�u�����FGraphServiceClient�N���X�iMicrosoft.Graph���O��ԁj
�֘ATIPS�F�d�q���[���𑗐M����ɂ́H�iMailKit�ҁj�m.NET 4.5�AC#�^VB�n
�֘ATIPS�F�\���F�C���X�^���X���Ɠ����Ƀv���p�e�B��ݒ肷��ɂ́H�mC#�^VB�n
�֘ATIPS�F�\���F�R���N�V�����̃C���X�^���X���Ɠ����ɗv�f��lj�����ɂ́H�mC#�^VB�n
�֘ATIPS�F�\���F�N���X�����������ɐÓI���\�b�h���Ăяo���ɂ́H�mC# 6.0�n
�֘ATIPS�FVB.NET�ŃN���X�����ȗ����ă��\�b�h��萔�𗘗p����ɂ́H
�֘ATIPS�F���l���E�l�߂�0���߂ŕ�������ɂ́H�mC#�AVB�n
�֘ATIPS�FVisual Studio�ŃR���\�[���E�A�v���P�[�V�����̃f�o�b�O���s���ɃR�}���h�E�v�����v�g����Ȃ��悤�ɂ���ɂ́H
�u.NET TIPS�v
Copyright© Digital Advantage Corp. All Rights Reserved.